I have code that works with .NET 4.5.1:
private void button20_Click(object sender, EventArgs e)
{
String fullFilePath = @"C:\HoldingPattern\INV_0000003_20140818135513_1725.xml";
string justFileName = Path.GetFileNameWithoutExtension(fullFilePath);
String uri = String.Format("http://localhost:21608/api/inventory/sendXML/platypup/platypup/{0}", justFileName);
SendXMLFile(fullFilePath, uri, 500);
}
public static string SendXMLFile(string xmlFilepath, string uri, int timeout)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.ContentType = "application/xml";
request.Method = "POST";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(xmlFilepath))
{
String line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
MessageBox.Show(sb.ToString());
byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());
if (timeout < 0)
{
request.ReadWriteTimeout = timeout;
request.Timeout = timeout;
}
request.ContentLength = postBytes.Length;
try
{
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = null; //<= uncomment for older versions of .NET
try
{
response = (HttpWebResponse)request.GetResponse();
}
finally
{
IDisposable disposableResponse = response as IDisposable;
if (disposableResponse != null) disposableResponse.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
request.Abort();
return string.Empty;
}
}
}
...but the corresponding code targeting .NET 1.1 and the Compact Framework -- altered only as necessary in order to compile -- does not (it hangs)? The as-close-as-possible version of SendXML() differs only because of these compiler err msgs:
0) '*System.Net.HttpWebRequest' does not contain a definition for 'ReadWriteTimeout*'
1) '*System.Text.StringBuilder' does not contain a definition for 'AppendLine*'
Because of them, I commented out "request.ReadWriteTimeout = timeout;" and I changed "sb.AppendLine(line);" to "sb.Append(line);"
The other difference is the assignment of the REST URI, which becomes:
String uri = String.Format("http://192.168.125.50:21608/api/inventory/sendXML/platypup/platypup/{0}", justFileName);
(the difference being the IP address is used instead of localhost, as that is necessary from the handheld device).
What specifically of those minor differences could be causeing it to fail? ...and more to the counterpoint, what need I do to tweak it enough for it to work? My best guess is I need to emulate the StringBuilder.AppendLine() calls - does anybody know in what exact way that should be done, provided that is a good theory?
UPDATE
Note: I have now tried prepending this to the StringBuilder:
sb.Append("<data><![CDATA[");
...and appending this:
sb.Append("]></data>");
...based on the answer by Andras Zoltan here, and I do get further - it no longer hangs. But I get an IOException...
UPDATE 2
I also tried appending a "newline" to the StringBuilder like so:
sb.Append("\r\n");
...but it made no difference.
UPDATE 3
With this code, too (which I got from tcarvin's link below, except that I am using the .NET 1.1 version of the code):
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(uri);
myHttpWebRequest.AllowWriteStreamBuffering=false;
string postData = "Bla bla bla>"; // TODO: if this works, replace it with the real data
myHttpWebRequest.Method="POST";
ASCIIEncoding encodedData=new ASCIIEncoding();
byte[] byteArray=encodedData.GetBytes(postData);
myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength=byteArray.Length;
Stream newStream=myHttpWebRequest.GetRequestStream();
newStream.Write(byteArray,0,byteArray.Length);
newStream.Close();
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
return myHttpWebResponse.StatusDescription;
...I still get the "400 - Bad Request" err msg.
UPDATE 4
I saw something that made me think I needed to prepend backwhacks to the filepath, so I changed this code:
using (StreamReader sr = new StreamReader(xmlFilepath))
...to this:
String s = String.Format("\\{0}", xmlFilepath);
using (StreamReader sr = new StreamReader(s))
...but I'm still "rewarded" with a "400 - Bad Request" err msg with that, too...
UPDATE 5
Changing the code from Update 3 to this:
UTF8Encoding encodedData = new UTF8Encoding();
//ASCIIEncoding encodedData=new ASCIIEncoding();
byte[] byteArray=encodedData.GetBytes(postData);
//myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
myHttpWebRequest.ContentType = "application/xml";
...also makes no difference - still get the "400 Bad Request" err msg returned.