C# Windows Phone 8的Http Post Get响应错误

C# Windows Phone 8的Http Post Get响应错误,c#,sdk,http-post,windows-phone,C#,Sdk,Http Post,Windows Phone,我从我原来的帖子()转载了第二个问题,因为我的主要问题是alreayd回答的 这是我在@Hunter McMillen的帮助下更新的代码。。我现在正在尝试从服务器获取responseCallback。问题在于第二个using语句中的GetResponseCallback=>(HttpWebResponse)httpWebRequest.EndGetResponse(GetResponseCallback)行正在显示 An exception of type 'System.Net.WebExce

我从我原来的帖子()转载了第二个问题,因为我的主要问题是alreayd回答的

这是我在@Hunter McMillen的帮助下更新的代码。。我现在正在尝试从服务器获取responseCallback。问题在于第二个using语句中的
GetResponseCallback=>(HttpWebResponse)httpWebRequest.EndGetResponse(GetResponseCallback)
行正在显示

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.
这个错误以前在我使用第一个示例时发生过。有人知道如何解决这个问题吗

  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()); });
        }
    }
private静态异步void HttpPostData(){
字符串url=”http://www.mytunnel.com/api/purchases";
HttpWebRequest HttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType=“text/plain”;
//httpWebRequest.ContentType=“应用程序/x-www-form-urlencoded”;
httpWebRequest.AllowWriteStreamBuffering=true;
httpWebRequest.Method=“POST”;
//httpWebRequest.ContentLength=jsonAsBytes.Length;
试一试{
使用(var stream=await Task.Factory.fromsync(httpWebRequest.BeginGetRequestStream,httpWebRequest.EndGetRequestStream,null))
{
byte[]jsonAsBytes=Encoding.UTF8.GetBytes(“{\'data\':\'json\'}”);
wait stream.WriteAsync(jsonAsBytes,0,jsonAsBytes.Length);
}
}
catch(异常e){Debug.WriteLine(e.Message);}
httpWebRequest.BeginGetResponse(新的异步回调(ReadWebRequestCallback),httpWebRequest);
}
私有静态void ReadWebRequestCallback(IAsyncResult callbackResult)
{ 
HttpWebRequest myRequest=callbackResult.AsyncState为HttpWebRequest;
尝试
{
HttpWebResponse=myRequest.EndGetResponse(callbackResult)作为HttpWebResponse;
使用(StreamReader sr=newstreamreader(response.GetResponseStream()))
{
字符串s=sr.ReadToEnd();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(()=>{MessageBox.Show;});
}
}
捕获(WebException webExcp)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(()=>{MessageBox.Show(webExcp.ToString());});
}
}

将方法更改为
“获取”
,因为我认为您没有将任何数据发布到表单

将方法更改为
“获取”
,因为我认为您没有将任何数据发布到表单

我最终使用了WebClient。代码更加简洁,我阅读了我在原始帖子中提供的示例链接底部的注释。Microsoft的示例表单实际上是从stackoverflow post复制的。。而且它不起作用(不要相信MS docs)

这是我的代码和一个教程链接(),供所有遇到相同问题的人使用。。如果这对你有帮助,请帮我竖起大拇指

  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));
        }
    }
private void httpPostData()
{
WebClient WebClient=新的WebClient();
webClient.Headers[HttpRequestHeader.ContentType]=“应用程序/json”;
var uri=新的uri(“http://www.sample.com/api/purchases“,乌里金。绝对);
webClient.Headers[HttpRequestHeader.ContentLength]=postData.Length.ToString();
webClient.AllowWriteStreamBuffering=true;
webClient.Encoding=System.Text.Encoding.UTF8;
UploadStringAsync(uri,“POST”,postData.ToString());
webClient.UploadStringCompleted+=新的UploadStringCompletedEventHandler(postComplete);
系统线程线程睡眠(200);
}
私有void postComplete(对象发送方,上载StringCompletedEventArgs e)
{
reset.Set();
响应结果=JsonConvert.DeserializeObject(e.result);
if(result.success==true)
{
DispatchCommandResult(新的PluginResult(PluginResult.Status.OK,package_id));
}
}

我最终改用了WebClient。代码更加简洁,我阅读了我在原始帖子中提供的示例链接底部的注释。Microsoft的示例表单实际上是从stackoverflow post复制的。。而且它不起作用(不要相信MS docs)

这是我的代码和一个教程链接(),供所有遇到相同问题的人使用。。如果这对你有帮助,请帮我竖起大拇指

  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));
        }
    }
private void httpPostData()
{
WebClient WebClient=新的WebClient();
webClient.Headers[HttpRequestHeader.ContentType]=“应用程序/json”;
var uri=新的uri(“http://www.sample.com/api/purchases“,乌里金。绝对);
webClient.Headers[HttpRequestHeader.ContentLength]=postData.Length.ToString();
webClient.AllowWriteStreamBuffering=true;
webClient.Encoding=System.Text.Encoding.UTF8;
UploadStringAsync(uri,“POST”,postData.ToString());
webClient.UploadStringCompleted+=新的UploadStringCompletedEventHandler(postComplete);
系统线程线程睡眠(200);
}
私有void postComplete(对象发送方,上载StringCompletedEventArgs e)
{
reset.Set();
响应结果=JsonConvert.DeserializeObject(e.result);
if(result.success==true)
{
DispatchCommandResult(新的PluginResult(PluginResult.Status.OK,package_id));
}
}

在调用第二个方法之前,我尝试将httpWebRequest.method=“GET”放在正确的位置,但这会引发调试器中断。取出内容类型alsoError消息“由于httpWebRequest对象的状态,无法设置方法”。在调用第二个方法之前,我尝试将httpWebRequest.method=“GET”放在正确的位置,但它使我遇到调试器中断。取出内容类型alsoError消息“由于HttpWebRequest对象的状态,无法设置方法”。WebClient在Windows Phone 8.1中似乎不是有效类。WebClient似乎不是有效类