当header包含非ascii内容时,System.Net.Browser.ClientHttpWebRequest.EndGetResponse抛出System.ArgumentException

时间:2011-09-22 04:15:27

标签: c# windows-phone-7 httpwebrequest

我有一个用我的Windows Phone 7项目编写的功能。它将跟踪每个HttpWebRequest的内容标题,如果内容类型是文档(如Web邮件中的pdf附件),它将下载它。当文件名是英文时,该功能正常工作。但是,当文件名是非ascii时,比如说中文和日文,它会在(HttpWebResponse)m_responseRequest.EndGetResponse(m_responseCallbackAsyncResult)中抛出一个System.ArgumentException。怎么解决?这对我很重要,因为我需要处理很多用中文命名的文件。以下是我的代码:

    private void _checkHeader(string m_uri)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_uri);
        request.Method = "POST";

        request.BeginGetRequestStream((asynchronousResult) =>
        {
            var m_readCallBackrequest = (HttpWebRequest)asynchronousResult.AsyncState;
            m_readCallBackrequest.BeginGetResponse((m_responseCallbackAsyncResult) =>
            {
                var m_responseRequest = (HttpWebRequest)m_responseCallbackAsyncResult.AsyncState;
                try
                {
                    var resp = (HttpWebResponse)m_responseRequest.EndGetResponse(m_responseCallbackAsyncResult);
                    Debug.WriteLine(resp.Headers.ToString());

                }
                catch (WebException) { }
            }, m_readCallBackrequest);
        }, request);
    }

异常细节:

  

System.ArgumentException未处理     消息=“”     堆栈跟踪:          在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state)          在System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)          在FotomaxWP71.ViewModel.WebViewModel。< checkHeader> b _d(IAsyncResult m_responseCallbackAsyncResult)          在System.Net.Browser.ClientHttpWebRequest。<> c_ DisplayClassa.b _8(Object state2)          在System.Threading.ThreadPool.WorkItem.WaitCallback_Context(对象状态)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ThreadPool.WorkItem.doWork(Object o)          在System.Threading.Timer.ring()     InnerException:System.ArgumentException          消息= [net_WebHeaderInvalidControlChars]   参数:   调试资源字符串不可用。通常,密钥和参数提供了足够的信息来诊断问题。见http://go.microsoft.com/fwlink/?linkid=106663&Version=4.7.60408.0&File=System.Net.dll&Key=net_WebHeaderInvalidControlChars   参数名称:名称          堆栈跟踪:               在System.Net.ValidationHelper.CheckBadWebHeaderChars(String name,Boolean isHeaderValue)               at System.Net.WebHeaderCollection.set_Item(String name,String value)               在System.Net.Browser.HttpWebRequestHelper.AddHeaderToCollection(WebHeaderCollection headerCollection,String headerName,String headerValue)               在System.Net.Browser.HttpWebRequestHelper.ParseHeaders(Uri requestUri,SecurityCriticalDataForMultipleGetAndSet`1 headers,WebHeaderCollection collection,Boolean removeHttpOnlyCookies,HttpStatusCode& status,String& statusDescription)               在System.Net.Browser.ClientHttpWebRequest.Progress(Object sender,EventArgs e)               在MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,Delegate handlerDelegate,Object sender,Object args)               在MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex,String eventName)

非常感谢!

1 个答案:

答案 0 :(得分:1)

try{}中的代码更改为:

var resp = (HttpWebResponse)m_responseRequest.EndGetResponse(m_responseCallbackAsyncResult);
byte[] buf = new byte[resp.Headers.ToString().Length];
buf = resp.Headers.ToString().ToCharArray();
Debug.WriteLine(buf);