WP7 HttpWebRequest POST, server returns "{ result: 'ok' }\" in emulator, but "error: NotFound" on device
I'm trying to do a very basic http POST to a web service from my Windows Phone 7 app. I know the web service works fine because I'm using it for three other mobile platforms.
I've modified the C# example from http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx
string boundary = DateTime.Now.Ticks.ToString();
private void POST_TEST(object sender, RoutedEventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constants.JSON_URL_PREFIX + Settings.Settings.DeviceID + "/inquiry/new/");
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
StringBuilder postData = new StringBuilder();
postData.Append("--" + boundary + "\r\n");
postData.Append("Content-Disposition: form-data; name=\"body\"\r\n\r\n");
postData.Append("test 123");
postData.Append("\r\n--" + boundary + "\r\n");
byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToString());
postStre开发者_如何学JAVAam.Write(byteArray, 0, postData.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
}
I'm receiving "{ result: 'ok' }" from the server when I run this in the emulator, but "error: NotFound" when I run it on my Samsung Focus. I'm assuming this has to do with the way a string is converted to a byte[] on the phone versus a desktop computer.
Any ideas on a fix? Maybe this is a known error that I never ran across searching the web for an answer?
I finally figured out that my phone's Unique ID / Device ID has a forward-slash in it. This caused a problem when I constructed the web service Uri at:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constants.JSON_URL_PREFIX + Settings.Settings.DeviceID + "/inquiry/new/");
Uri emulator example: http://www.blah.com/abc_123=/inquiry/new/
Uri device example: http://www.blah.com/abc/123=/inquiry/new/
Obviously, it worked fine in the emulator because the emulator's device ID doesn't have a forward-slash in it. Essentially, the Uri I was trying to post to was incorrect.
Kind of an obscure bug...
Thanks Opena and Matt. Your suggestions gave me things to try and eventually helped me see the problem.
I use this solution on an app but my postData looks like postData.Append("test=123")
. I don't use boundaries (I thought boundaries were only for mails ?)
Also my request.ContentType is request.ContentType = "application/x-www-form-urlencoded";
精彩评论