所以我有一個功能,像這樣:C#WebResponse流丟失字節?
private String SendRequest(String jsonRequest)
{
WebRequest webRequest = WebRequest.Create(_url);
byte[] paramBytes = Encoding.UTF8.GetBytes(jsonRequest);
byte[] responseBytes;
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.ContentLength = paramBytes.Length;
webRequest.Headers.Add("X-Transmission-Session-Id", _sessionId);
using (Stream oStream = webRequest.GetRequestStream())
{
oStream.Write(paramBytes, 0, paramBytes.Length);
}
WebResponse webResponse = webRequest.GetResponse();
using (Stream iStream = webResponse.GetResponseStream())
{
responseBytes = new byte[webResponse.ContentLength];
iStream.Read(responseBytes, 0, (int) webResponse.ContentLength);
}
return Encoding.UTF8.GetString(responseBytes);
}
的問題是,在iStream.Read()階段,一些字節都將丟失。使用wireshark顯示所有的字節被髮送到這臺機器,但是.net在這個路上的某處丟失了。例如,在我當前的調試會話中,其中webResponse.ContentLength = 4746字節[3949]至字節[4745]均爲0,但它們應該填充。因此,UTF8 JSON字符串提前切斷,我無法對JSON進行反序列化。
我認爲代碼非常清晰,我看不到錯在哪裏去釋放這些字節。
感謝您的幫助!