0

我在 httpWebRequest 中从来都不是很好。总是首选 WebClient,但......绝望的时间,绝望的措施。

private static void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                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();
                //allDone.Set();
            }
            catch (WebException exception)
            {

            }
        } 

我可以看到(当我下一个断点时)responseString 是漂亮的 JSON,但我无法将其打印出来(MessageBox 等),因为我的应用程序会中断。

你能告诉我如何处理吗?干杯

4

2 回答 2

1

添加魔法

Dispatcher.BeginInvoke(() => { MessageBox.Show("OK"); });

并且像魅力一样工作:)

于 2013-08-21T19:31:03.557 回答
0

试试这个

 using System.Net;
    public void sendRequest() 
    {
    WebRequest webRequest;
                    webRequest = WebRequest.Create(XXXXXXXXXX); 
                    webRequest.Method = "POST";
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                    webRequest.BeginGetRequestStream(newAsyncCallback(GetRequestStreamCallback), webRequest);
    }

    public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
            {
                    webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                    Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
                    string postData = "Test";
                    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                    postStream.Write(byteArray, 0, byteArray.Length);
                    postStream.Close();
                    webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
                }
    public void GetResponseCallback(IAsyncResult asynchronousResult)
            {
                try
                {
                    HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                    HttpWebResponse response;
                    response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
                    Stream streamResponse = response.GetResponseStream();
                    StreamReader streamReader = new StreamReader(streamResponse);
                    var Response = streamReader.ReadToEnd();
                    streamResponse.Close();
                    streamReader.Close();
                    response.Close();
                    if (Response == "")
                    {
                       //show some error msg to the user                    

                    }
                    else
                    {                   
                      //Your response will be available in "Response"                        
                    }
                }
                catch (WebException)
                {
                    //error    
                }
            }

这里的 XXXXXXXX 表示您的 url,其中包含发送到服务器的数据。

于 2013-08-21T04:44:40.980 回答