System.ArgumentException: [net_WebHeaderInvalidControlChars] in Windoows Phone

☆樱花仙子☆ 提交于 2019-12-11 05:58:45

问题


I am using HttpWebRequest to fetch some twitter-API-like Json resource, the scenario is that after logining the app with XAuth-Authentication, the app will use an API(http://api.xxx.com/account/notification.json) to get the respose Json. My HttpWebRequest Method is as followed:

public void Request(string url)
        {
            var request = (HttpWebRequest)HttpWebRequest.Create(url);

            IAsyncResult result = null;
            result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request);
        }

        private void ResponseCallback(IAsyncResult result)
        {
            try
            {
                var request = (HttpWebRequest)result.AsyncState;
                var response = request.EndGetResponse(result);


                using (var stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {

                        if (CallBack != null)
                        {
                            var str = reader.ReadToEnd();
                            CallBack(CleanInvalidXmlChars(str));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    CallBack(ex.ToString());
                });
            }
        } 

At the most common situation everything goes well. Until some users told me that they always got an exception. Then I use Debug to find out that the Exception trace is :

"System.ArgumentException ---> System.ArgumentException: [net_WebHeaderInvalidControlChars]\r\nArguments:\r\nDebugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=4.7.60408.0&File=System.Net.dll&Key=net_WebHeaderInvalidControlChars \r\nParameter name: name\r\n   at System.Net.ValidationHelper.CheckBadWebHeaderChars(String name, Boolean isHeaderValue)\r\n   at System.Net.WebHeaderCollection.set_Item(String name, String value)\r\n   at System.Net.Browser.HttpWebRequestHelper.AddHeaderToCollection(WebHeaderCollection headerCollection, String headerName, String headerValue)\r\n   at System.Net.Browser.HttpWebRequestHelper.ParseHeaders(Uri requestUri, SecurityCriticalDataForMultipleGetAndSet`1 headers, WebHeaderCollection collection, Boolean removeHttpOnlyCookies, HttpStatusCode& status, String& statusDescription)\r\n   at System.Net.Browser.ClientHttpWebRequest.Progress(Object sender, EventArgs e)\r\n   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)\r\n   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)\r\n\r\n   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)\r\n   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\r\n   at FanfouSDK.Utiltiy.HttpGetMethod.ResponseCallback(IAsyncResult result)\r\n   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)\r\n   at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ThreadPool.WorkItem.doWork(Object o)\r\n   at System.Threading.Timer.ring()\r\n"

It seems to be the some problem in here(System.Net.Browser.ClientHttpWebRequest.EndGetResponse throw System.ArgumentException when header contains non-ascii content)

Then I use the Fiddler to trace the webresponse, found that the server returned the expected Json string, but still caused an excetion.

The expected Json string:

Everythis is OK, but still cause a net_WebHeaderInvalidControlChars exception, is it because of the X-Authuser?

I've worked hard to find a solution, but just failed. Any suggestions will be appreciated. Thanks.

P.S: It's a little strange that when I run my app in my real device, there is no exception, but an exception will come out in the emulator and the other's real device.


回答1:


I suspect this is because of Chinese characters present in the header (X-AuthUser). This problem was also described here with Cyrillic characters, so it is highly likely to be the same scernario.



来源:https://stackoverflow.com/questions/10116963/system-argumentexception-net-webheaderinvalidcontrolchars-in-windoows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!