1

私の最初の質問はすでに回答されていたので、元の投稿( Windows Phone8のHttpPost)からこの2番目の質問を再投稿しています。

これは、@ HunterMcMillenの助けを借りて更新されたコードです。サーバーからresponseCallbackを取得しようとしています。問題はGetResponseCallback => (HttpWebResponse)httpWebRequest.EndGetResponse(GetResponseCallback)、2番目のusingステートメントの行です。

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()); });
        }
    }
4

2 に答える 2

5

代わりにWebClientを使用することになりました。コードははるかにクリーンで、元の投稿で提供したサンプルリンクの下部にあるコメントを読みました。Microsoftのサンプルフォームは実際にはstackoverflowの投稿からコピーされたものであり、機能しません(MSドキュメントを信頼しないでください)。

これが私のコードと同じ問題を抱えているすべての人のためのチュートリアルリンク(http://www.daveamenta.com/2008-05/c-webclient-usage/)です。これがあなたを助けるなら私のために親指を立ててください。

  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));
        }
    }
于 2013-02-26T04:01:59.213 に答える
0

"GET"フォームにデータを投稿しないと思うので、メソッドをに変更します

于 2013-02-05T08:34:58.213 に答える