[英]Why is IE6 sending me NULL characters (0x00) as JSON POST data?
嘗試反序列化從jQuery.ajax()發送的JSON數據時,我的(C#)服務器代碼中偶爾出現異常。 POST數據完全是NULL字符(二進制,0x00)。 我在日志和文本模式下記錄了所有來自錯誤的POST數據,它看起來像空格:
" "
但是在二進制模式下,它是:
00 00 00 00 00 00 00 00 00 00
同一位用戶已記錄了3次相同的錯誤,大約間隔15分鍾。
可能會發生什么? 我已經在Google上搜索了,卻找不到類似的東西。
這是JSON反序列化器的實際錯誤:
System.ArgumentException: Invalid JSON primitive: .
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
用戶代理:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; MS-RTC LM 8; InfoPath.2)
內容類型:
application/json
另一個線索:在我們的全局錯誤處理程序中,我們嘗試向用戶編寫響應消息(例如“很抱歉,我們正在調查中。”)。 當我們嘗試為該用戶執行此操作時,每次都有相同的異常:
System.Web.HttpException (0x80070057): The remote host closed the connection. The error code is 0x80070057.
at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
at System.Web.HttpResponse.Flush(Boolean finalFlush)
at System.Web.HttpResponse.End()
這是我們讀取數據的方式(我不得不復制/粘貼一些我們的庫代碼):
[Ajax]
[AsyncTimeout(300001)] // 5 minutes
[ValidateInput(false)] // We want to pass in XML via POST
public void CommandAsync()
{
AsyncManager.OutstandingOperations.Increment();
// Reads the main body of a request, without closing the
// stream. The advantage of not closing the stream is that it can
// be re-read later, particularly by the Global.asax.cs error
// handler.
// It seems that if the content type is not form-url-encoded
// then the input stream needs to be reset. Weird.
Request.InputStream.Position = 0;
byte[] content = new byte[Request.ContentLength];
Request.InputStream.Read(content, 0, Request.ContentLength);
string dataStr = Encoding.UTF8.GetString(content);
if (dataStr.Length != 0) {
var data = serializer.Deserialize<Dictionary<string, object>>(dataStr);
// do something with data
}
}
[編輯]
埃里克(Eric)是正確的,因為IE在流中沒有發送任何數據。
因此,對我們來說,解決方案是將數組縮小到讀取的值:
int content_length = request.ContentLength;
byte[] content = new byte[content_length];
int read = request.InputStream.Read(content, 0, content_length);
if (read < content_length)
Array.Resize(ref content, read);
或者,我們可以簡單地使用StreamReader(request.InputStream).ReadToEnd(),它的效率會降低一點,但永遠不會出現此錯誤。
謝謝埃里克,你是對的!
在Internet Explorer中取消AJAX發布請求后,它仍將它們發送到服務器,但沒有實際內容,並且標頭中的內容長度不正確。
我們使用以下內容閱讀內容:
request.InputStream.Position = 0;
byte[] content = new byte[request.ContentLength];
request.InputStream.Read(content, 0, request.ContentLength);
它創建了一個\\ 0的字節數組,因為沒有內容可以覆蓋已初始化的數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.