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

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 に答える