【问题标题】:How to fix System.ArgumentException in HttpWebResponse?如何修复 HttpWebResponse 中的 System.ArgumentException?
【发布时间】:2012-11-15 22:13:24
【问题描述】:

在为我的 WindowsPhone 应用程序接收 HttpWebResponse 时,我遇到了这个异常。我该如何解决这个问题。它经常发生,但我需要确保我的应用程序在发生时不会崩溃。请查看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)
            {
            }
    }

【问题讨论】:

  • 它说 ArgumentException 并崩溃。如果你愿意,我可以粘贴代码。它类似于基本的 httpwebrequest 和响应代码。很抱歉,您能解释一下 repro 是什么意思吗?
  • 还有马特你能解释一下我想弄清楚的东西(与这个问题不同)。我的应用程序就像一个推文提要阅读器。但是随着使用,应用程序的响应速度会在 wp8 模拟器中变慢。我认为这是模拟器的内存问题。我对吗?我无法测试它,因为我还没有设备。请帮我解决这些问题。
  • "repro" = "reproduction" :我怎样才能重现你所看到的?如果你有一个单独的问题,你应该这样问,而不是通过 cmets。另请参阅tinyurl.com/so-list
  • 我现在已经添加了代码。我不知道您如何重现它,因为当服务器关闭时会发生这种情况。 :(
  • 修复这个错误有什么进展吗?我遇到了完全相同的问题,它同时发生在 WebClient、HttpWebRequest 或 HttpClient 上。

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


【解决方案1】:

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

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

对我来说,造成问题的是 Google App Engine 的自定义 User-Agent 标头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2012-12-13
    • 1970-01-01
    • 2011-09-25
    • 2016-07-08
    • 1970-01-01
    相关资源
    最近更新 更多