System.Net.Browser.ClientHttpWebRequest.EndGetResp

2019-05-20 21:28发布

问题:

I have a function that written in my Windows Phone 7 project. It is going to trace the content header of each HttpWebRequest and if the content-type is document(like a pdf attachment in Web mail), it will download it. The function worked fine when the file name is English. However, when the file name is non-ascii, say Chinese and Japanese, it will throw an System.ArgumentException in (HttpWebResponse)m_responseRequest.EndGetResponse(m_responseCallbackAsyncResult). How to fix it? It is important to me as I need to handle a lot of file named in Chinese. The following is my code:

    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);
    }

And the exception detail:

System.ArgumentException was unhandled Message="" StackTrace: at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at FotomaxWP71.ViewModel.WebViewModel.<checkHeader>b_d(IAsyncResult m_responseCallbackAsyncResult) at System.Net.Browser.ClientHttpWebRequest.<>c_DisplayClassa.b_8(Object state2) at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadPool.WorkItem.doWork(Object o) at System.Threading.Timer.ring() InnerException: System.ArgumentException Message=[net_WebHeaderInvalidControlChars] Arguments: Debugging 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 Parameter name: name StackTrace: at System.Net.ValidationHelper.CheckBadWebHeaderChars(String name, Boolean isHeaderValue) at System.Net.WebHeaderCollection.set_Item(String name, String value) at System.Net.Browser.HttpWebRequestHelper.AddHeaderToCollection(WebHeaderCollection headerCollection, String headerName, String headerValue) at System.Net.Browser.HttpWebRequestHelper.ParseHeaders(Uri requestUri, SecurityCriticalDataForMultipleGetAndSet`1 headers, WebHeaderCollection collection, Boolean removeHttpOnlyCookies, HttpStatusCode& status, String& statusDescription) at System.Net.Browser.ClientHttpWebRequest.Progress(Object sender, EventArgs e) at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Thanks a lot!

回答1:

change the code in try{} to this:

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