在写入所有字节之前,无法关闭流(GoodData API) [英] Cannot close stream until all bytes are written (GoodData API)
问题描述
我已经做了一些搜索,大多数人似乎在发送大量数据时都达到了这个目的,但是我没有.
I've done a bit of searching, and most people seem to hit this when sending larger amounts of data, but I'm not.
我正在通过以下方式向API发出请求:
I'm making a request to an API with the following:
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);
}
该例外发生在最后一个括号中:
The exception occurs on the last closing bracket:
在写入所有字节之前无法关闭流.说明:当前Web执行期间发生未处理的异常要求.请查看堆栈跟踪以获取有关以下内容的更多信息:错误及其在代码中的起源.
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.
异常详细信息:System.IO.IOException:无法关闭流,直到所有字节都被写入.
Exception Details: System.IO.IOException: Cannot close stream until all bytes are written.
源错误:
第176行:{第177行:
writer.Write(byteArray);178行:} 179行:
字符串responseContent;第180行:使用(var response =request.GetResponse()为System.Net.HttpWebResponse)
Line 176: { Line 177:
writer.Write(byteArray); Line 178: } Line 179:
string responseContent; Line 180: using (var response =
request.GetResponse() as System.Net.HttpWebResponse)
源文件:c:\ Users \ james.billings \ Documents \ Visual Studio2012 \ Projects \ PhoneMonitor \ PhoneMonitor \ GoodData \ DataRetriever.cs
行:178
Source File: c:\Users\james.billings\Documents\Visual Studio
2012\Projects\PhoneMonitor\PhoneMonitor\GoodData\DataRetriever.cs
Line: 178
堆栈跟踪:
[IOException:在写入所有字节之前,无法关闭流.]
System.Net.ConnectStream.CloseInternal(Boolean internalCall,Boolean正在中止)+604
[IOException: Cannot close stream until all bytes are written.]
System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean
aborting) +604
[WebException:请求被中止:请求被取消.]
System.Net.ConnectStream.CloseInternal(Boolean internalCall,Boolean正在中止)+6443608
System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExStatecloseState)+37 System.Net.ConnectStream.Dispose(布尔处理)+44
[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/
Any clues? Specifically I'm trying to use the GoodData API call xtab2/executor3 - see http://docs.gooddata.apiary.io/
如果我删除" set ContentLength
",则会收到400错误的请求.如果我像早期的GET请求一样设置为0(可以正常工作),则会收到一个错误,我尝试写的内容超出了缓冲区的长度,这是有道理的.
If I remove the "set ContentLength
" I get a 400 Bad Request. If I set to 0 like earlier GET requests (which work fine) I get an error that I tried to write more than the length of the buffer, which makes sense.
有点烂!
推荐答案
Having looked over the docs - System.IO.StreamWriter.Write() - There does not appear to be a method for writing bytes.
与签名匹配的唯一方法是- StreamWriter.Write(Object)
.但是,这会在对象上调用 ToString()
并写入输出.这不是你想要的.
The only method that matches the signature is - StreamWriter.Write(Object)
. This however calls ToString()
on the object and writes the output; Which is not what you want.
在设置输出缓冲区时;流正在等待该缓冲区被填充.但是, Object.ToString()
可能不会填充此缓冲区,因此会导致错误.
As you are setting an output buffer; the stream is waiting for this buffer to be filled. However, the Object.ToString()
will likely not fill this buffer and hence the error.
使用 BinaryWriter
, BufferedStream
或其他支持 byte []
编写的
Use BinaryWriter
, BufferedStream
or another that supports byte[]
writing.
这篇关于在写入所有字节之前,无法关闭流(GoodData API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!