如何修复HttpWebResponse中的System.ArgumentException? [英] How to fix System.ArgumentException in HttpWebResponse?

查看:146
本文介绍了如何修复HttpWebResponse中的System.ArgumentException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的WindowsPhone应用程序接收到HttpWebResponse时,我将遇到此异常.我应该如何解决这个问题.它经常发生,但是我需要确保我的应用程序不会崩溃.请查看屏幕截图.

I am facing this exception when receiving HttpWebResponse for my WindowsPhone app. How am I supposed to fix this. It happens very often but I need to make sure my app doesn't crash if it happens. Please have a look at the 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

It also shows in the InnerException the message as Specified value has invalid HTTP Header characters. Parameter name: name

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

Please help. I don't know why webRequest.EndGetResponse(asynchronousResult) is not able to read the response. Is there an alternative?

更新 开始请求:

_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)
            {
            }
    }

推荐答案

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

Put a breakpoint in your catch block, and look at the lower level stacks, and look for System.Net.ni.dll!System.Net.WebHeaderCollection.this[string].set(string name, string value).

在局部变量中,您可以查看解析失败的那个特定值.

In the local variables, you can see for which particular value, the parse is failing.

对我而言,正是Google App Engine的自定义User-Agent标头导致了问题.

For me, It was Google App Engine's custom User-Agent header which was creating the problem.

这篇关于如何修复HttpWebResponse中的System.ArgumentException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆