我已經完成了一些搜索,大多數人在發送大量數據時似乎遇到了這種情況,但我不是。無法關閉流,直到寫入所有字節(GoodData API)
我正在請求的API有以下:
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
request.Headers.Add("Cookie", "$Version=0; GDCAuthTT=" + TToken + "; $Path=/gdc/account");
//generate request parameters
ReportRequest.RootObject reportRequest = new ReportRequest.RootObject();
reportRequest.report_req = new ReportRequest.ReportReq();
reportRequest.report_req.reportDefinition = ReportLink;
JavaScriptSerializer serializer = new JavaScriptSerializer();
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(serializer.Serialize(reportRequest));
request.ContentLength = byteArray.Length;
using (var writer = new System.IO.StreamWriter(request.GetRequestStream(), Encoding.UTF8, byteArray.Length))
{
writer.Write(byteArray);
}
唯一的例外發生在最後的右括號:
Cannot close stream until all bytes are written. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.IOException: Cannot close stream until all bytes are written.
Source Error:
Line 176: { Line 177:
writer.Write(byteArray); Line 178: } Line 179:
string responseContent; Line 180: using (var response = request.GetResponse() as System.Net.HttpWebResponse)Source File: c:\Users\james.billings\Documents\Visual Studio 2012\Projects\PhoneMonitor\PhoneMonitor\GoodData\DataRetriever.cs
Line: 178Stack Trace:
[IOException: Cannot close stream until all bytes are written.]
System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting) +604[WebException: The request was aborted: The request was canceled.]
System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting) +6443608
System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState) +37 System.Net.ConnectStream.Dispose(Boolean disposing) +44
任何線索?具體而言,我試圖使用GoodData API調用xtab2/executor3 - 請參閱http://docs.gooddata.apiary.io/
如果我刪除「set ContentLength
」,則會收到400錯誤請求。如果我像早期的GET請求那樣設置爲0(可以正常工作),我得到一個錯誤,我嘗試寫入超過緩衝區的長度,這很合理。
有點難住!
我認爲你是對的。這將教我在api網站上覆制和粘貼示例代碼!我已經改變它來寫字符串而不是字節。現在得到一個404,但我認爲這是因爲我現在給它一個假的價值,所以我會標記這個答案是正確的! – James
這就結束了大約4個小時的挫折感。謝謝Kami! :) – BVernon