title: System.Net.Browser.ClientHttpWebRequest.EndGetResponse throw System.ArgumentException when header contains non-ascii content
body: <p>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:</p>
  
  <pre><code>    private void _checkHeader(string m_uri)
      {
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_uri);
          request.Method = "POST";
  
          request.BeginGetRequestStream((asynchronousResult) =&gt;
          {
              var m_readCallBackrequest = (HttpWebRequest)asynchronousResult.AsyncState;
              m_readCallBackrequest.BeginGetResponse((m_responseCallbackAsyncResult) =&gt;
              {
                  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);
      }
  </code></pre>
  
  <p>And the exception detail:</p>
  
  <blockquote>
    <p>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.&lt;<em>checkHeader>b</em>_d(IAsyncResult m_responseCallbackAsyncResult)
           at System.Net.Browser.ClientHttpWebRequest.&lt;>c_<em>DisplayClassa.b</em>_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 <a href="http://go.microsoft.com/fwlink/?linkid=106663&amp;Version=4.7.60408.0&amp;File=System.Net.dll&amp;Key=net_WebHeaderInvalidControlChars" rel="nofollow">http://go.microsoft.com/fwlink/?linkid=106663&amp;Version=4.7.60408.0&amp;File=System.Net.dll&amp;Key=net_WebHeaderInvalidControlChars</a> 
    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&amp; status, String&amp; 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)</p>
  </blockquote>
  
  <p>Thanks a lot!</p>