我正在使用 HttpWebRequest 来获取一些类似 twitter-API 的 Json 资源,场景是使用 XAuth-Authentication 登录应用程序后,应用程序将使用 API(http://api.xxx.com/account/notification.json ) 以获取响应 Json。我的 HttpWebRequest 方法如下:
public void Request(string url)
{
var request = (HttpWebRequest)HttpWebRequest.Create(url);
IAsyncResult result = null;
result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request);
}
private void ResponseCallback(IAsyncResult result)
{
try
{
var request = (HttpWebRequest)result.AsyncState;
var response = request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
if (CallBack != null)
{
var str = reader.ReadToEnd();
CallBack(CleanInvalidXmlChars(str));
}
}
}
}
catch (Exception ex)
{
Deployment.Current.Dispatcher.BeginInvoke(delegate
{
CallBack(ex.ToString());
});
}
}
在最常见的情况下,一切都很顺利。直到一些用户告诉我他们总是遇到异常。然后我使用调试发现异常跟踪是:
"System.ArgumentException ---> System.ArgumentException: [net_WebHeaderInvalidControlChars]\r\nArguments:\r\nDebugging 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 \r\nParameter name: name\r\n at System.Net.ValidationHelper.CheckBadWebHeaderChars(String name, Boolean isHeaderValue)\r\n at System.Net.WebHeaderCollection.set_Item(String name, String value)\r\n at System.Net.Browser.HttpWebRequestHelper.AddHeaderToCollection(WebHeaderCollection headerCollection, String headerName, String headerValue)\r\n at System.Net.Browser.HttpWebRequestHelper.ParseHeaders(Uri requestUri, SecurityCriticalDataForMultipleGetAndSet`1 headers, WebHeaderCollection collection, Boolean removeHttpOnlyCookies, HttpStatusCode& status, String& statusDescription)\r\n at System.Net.Browser.ClientHttpWebRequest.Progress(Object sender, EventArgs e)\r\n at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)\r\n at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)\r\n\r\n at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)\r\n at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\r\n at FanfouSDK.Utiltiy.HttpGetMethod.ResponseCallback(IAsyncResult result)\r\n at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)\r\n at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ThreadPool.WorkItem.doWork(Object o)\r\n at System.Threading.Timer.ring()\r\n"
然后我使用 Fiddler 跟踪 webresponse,发现服务器返回了预期的 Json 字符串,但仍然导致异常。
预期的 Json 字符串: Everythis 是可以的,但仍然会导致 net_WebHeaderInvalidControlChars 异常,是不是因为 X-Authuser?
我一直在努力寻找解决方案,但只是失败了。任何建议将不胜感激。谢谢。
PS:有点奇怪,在我的真机上运行我的app时,并没有出现异常,但是在模拟器和对方的真机上都会出现异常。