如何在HttpWebResponse中修复System.ArgumentException?

时间:2012-11-28 05:51:35

标签: windows-phone-7 httpwebrequest windows-phone-7.1 httpwebresponse windows-phone-8

我在收到HttpWebResponse应用的WindowsPhone时遇到此异常。我该如何解决这个问题。它经常发生,但我需要确保我的应用程序在发生时不会崩溃。请查看screenshot

我的预期回应是

       Headers:-
       HTTP/1.1 500 Internal Server Error
       Date: Wed, 28 Nov 2012 06:41:24 GMT
       Content-Type: application/json
       Transfer-Encoding: chunked
       Connection: keep-alive
       Keep-Alive: timeout=30
       Set-Cookie: ...........; path=/
       Expires: Thu, 19 Nov 1981 08:52:00 GMT
       Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
       Pragma: no-cache
       Internal Server Error: 

       Json:-
       {"status":0,"error_code":1001,"data":{"msg":"Something went wrong. Please try again later. [error code 1001]"}}

它还会在InnerException消息中显示为Specified value has invalid HTTP Header characters. Parameter name: name

请帮忙。我不知道为什么webRequest.EndGetResponse(asynchronousResult)无法读取响应。还有其他选择吗?

更新 开始请求:

_webRequest.BeginGetRequestStream(new AsyncCallback(GetReqeustStreamCallback), _webRequest);

private void GetReqeustStreamCallback(IAsyncResult asynchronousResult)
    {
        if ((!ReqIdEnabled || Network.RequestUniqueId == this.RequestUniqueId))
        {
            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the stream request operation

            using (Stream postStream = webRequest.EndGetRequestStream(asynchronousResult))
            {

                // Add the post data to the web request
                postStream.Write(_postDataInBytes, 0, _postDataInBytes.Length);

                //postStream.Dispose();
            }

            // Start the web request
            webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
        }
    }

    private void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            try
            {
     //**throws Exception here when my server returns 503/500 and is not caught by the catch block below**
                using (HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult))  
                {
                    ReadResponse(response);
                }
            }
            catch (WebException ee)
            {
            }
    }

1 个答案:

答案 0 :(得分:0)

在catch块中放置一个断点,查看较低级别的堆栈,并查找System.Net.ni.dll!System.Net.WebHeaderCollection.this[string].set(string name, string value)

在局部变量中,您可以看到解析失败的特定值。

对我来说,这是谷歌应用引擎的自定义用户代理标题,它正在创建问题。