Http Post获取Windows Phone 8的响应错误 [英] Http Post Get Response Error for Windows Phone 8
问题描述
我将从我原来的帖子中重新发布第二个问题( Windows Phone 8的Http Post )因为我的主要问题得到了回答。
I am reposting this second question from my original post (Http Post for Windows Phone 8) because my primary question was alreayd answered.
这是我在@Hunter McMillen的帮助下更新的代码。我现在正试图从服务器获取responseCallback。问题是 GetResponseCallback => (HttpWebResponse)httpWebRequest.EndGetResponse(GetResponseCallback)
第二个使用语句中的行,显示
This is my updated code with the help of @Hunter McMillen.. I am now trying to get a responseCallback from the server. The problem is the GetResponseCallback => (HttpWebResponse)httpWebRequest.EndGetResponse(GetResponseCallback)
line in the second using statement, it is displaying
An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll but was not handled in user code
If there is a handler for this exception, the program may be safely continued.
在我使用第一个示例之前发生此错误。有谁知道如何解决这个问题?
This error occured before when I was using the first example. Does anyone know how to solve this?
private static async void HttpPostData(){
string url = "http://www.mytunnel.com/api/purchases";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/plain";
//httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.AllowWriteStreamBuffering = true;
httpWebRequest.Method = "POST";
//httpWebRequest.ContentLength = jsonAsBytes.Length;
try{
using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null))
{
byte[] jsonAsBytes = Encoding.UTF8.GetBytes("{ \"data\" : \"json\" }");
await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
}
}
catch (Exception e) { Debug.WriteLine(e.Message); }
httpWebRequest.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), httpWebRequest);
}
private static void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = callbackResult.AsyncState as HttpWebRequest;
try
{
HttpWebResponse response = myRequest.EndGetResponse(callbackResult) as HttpWebResponse;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
String s = sr.ReadToEnd();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(s); });
}
}
catch (WebException webExcp)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(webExcp.ToString()); });
}
}
推荐答案
我最终使用WebClient。代码更清晰,我阅读了原帖中提供的示例链接底部的注释。 Microsoft的示例表单实际上是从stackoverflow帖子中复制而来的。它不起作用(不信任MS文档)。
I ended up using WebClient instead. The code is much cleaner and I read the comments at the bottom from the sample link I provided in the original post. The sample form Microsoft was actually copied from a stackoverflow post.. and it doesn't work (don't trust MS docs).
这是我的代码和教程链接( http://www.daveamenta.com/2008-05/c-webclient-usage/ )对于遇到同样问题的每个人。如果对您有帮助,请为我竖起大拇指。
Here's my code and a tutorial link (http://www.daveamenta.com/2008-05/c-webclient-usage/)for everyone having the same problem.. Thumbs up for me please if this helps you.
private void httpPostData()
{
WebClient webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
var uri = new Uri("http://www.sample.com/api/purchases", UriKind.Absolute);
webClient.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();
webClient.AllowWriteStreamBuffering = true;
webClient.Encoding = System.Text.Encoding.UTF8;
webClient.UploadStringAsync(uri, "POST", postData.ToString());
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(postComplete);
System.Threading.Thread.Sleep(200);
}
private void postComplete(object sender, UploadStringCompletedEventArgs e)
{
reset.Set();
Response result = JsonConvert.DeserializeObject<Response>(e.Result);
if (result.success == true)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, package_id));
}
}
这篇关于Http Post获取Windows Phone 8的响应错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!