【发布时间】:2011-11-22 11:54:08
【问题描述】:
我有一个写在我的 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,对象状态) 在 System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 FotomaxWP71.ViewModel.WebViewModel.checkHeader>b_d(IAsyncResult m_responseCallbackAsyncResult) 在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClassa.b_8(对象状态 2) 在 System.Threading.ThreadPool.WorkItem.WaitCallback_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadPool.WorkItem.doWork(对象 o) 在 System.Threading.Timer.ring() 内部异常: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(字符串名称,布尔 isHeaderValue) 在 System.Net.WebHeaderCollection.set_Item(字符串名称,字符串值) 在 System.Net.Browser.HttpWebRequestHelper.AddHeaderToCollection(WebHeaderCollection headerCollection,字符串 headerName,字符串 headerValue) 在 System.Net.Browser.HttpWebRequestHelper.ParseHeaders(Uri requestUri、SecurityCriticalDataForMultipleGetAndSet`1 标头、WebHeaderCollection 集合、布尔型 removeHttpOnlyCookies、HttpStatusCode& 状态、String& 状态描述) 在 System.Net.Browser.ClientHttpWebRequest.Progress(对象发送者,EventArgs e) 在 MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,委托 handlerDelegate,对象发送者,对象参数) 在 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
非常感谢!
【问题讨论】:
-
也许,Web 服务器在 content-disposition 标头中未正确编码非 ascii 文件名。使用一些工具(例如 fiddler)来检查 Web 服务器响应标头以确定这是否是问题所在。请参阅此链接以了解此编码应该如何:motobit.com/help/scptutl/pa97.htm - 当然,只有当您对 Web 服务器有任何控制权时,这才能解决问题。
标签: c# windows-phone-7 httpwebrequest