2
votes

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
Perhaps, web server is not encoding non-ascii file names correctly in content-disposition header. Use some tool (e.g fiddler) to inspect web server response headers to determine if that's the issue. See this link to understand how this encoding is supposed to be: motobit.com/help/scptutl/pa97.htm - of course, this will solve issue only if you have any control over web server(s).VinayC

1 Answers

1
votes

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