即使没有互联网连接,HttpWebRequest 也会获取流
我正在使用下面的代码将字节数组发送到站点。为什么即使没有互联网连接,此代码也不会抛出异常?即使没有连接,我也能够获取流并能够写入它。我希望它在 Stream postStream = 处抛出异常request1.EndGetRequestStream(result)。任何人都知道为什么它会这样。
private void UploadHttpFile()
{
HttpWebRequest request = WebRequest.CreateHttp(new Uri(myUrl));
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.UserAgent = "Mozilla/4.0 (Windows; U; Windows Vista;)";
request.Method = "POST";
request.UseDefaultCredentials = true;
request.BeginGetRequestStream(GetStream, request);
}
private void GetStream(IAsyncResult result)
{
try
{
HttpWebRequest request1 = (HttpWebRequest)result.AsyncState;
using (Stream postStream = request1.EndGetRequestStream(result))
{
int len = postBody.Length;
len += mainBody.Length;
len += endBody.Length;
byte[] postArray = new byte[len + 1];
Encoding.UTF8.GetBytes(postBody.ToString()).CopyTo(postArray, 0);
Encoding.UTF8.GetBytes(mainBody).CopyTo(postArray, postBody.Length);
Encoding.UTF8.GetBytes(endBody).CopyTo(postArray, postBody.Length + mainBody.Length);
postStream.Write(postArray, 0, postArray.Length);
}
}
I am using below code to send a byte array to a site. Why this code is not throwing an exception even when there is no internet connection?.Even when no connection is there I am able to get stream and able to write to it.I expect it to throw an exception at Stream postStream = request1.EndGetRequestStream(result)
.Anyone got any idea why its behaving like this.
private void UploadHttpFile()
{
HttpWebRequest request = WebRequest.CreateHttp(new Uri(myUrl));
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.UserAgent = "Mozilla/4.0 (Windows; U; Windows Vista;)";
request.Method = "POST";
request.UseDefaultCredentials = true;
request.BeginGetRequestStream(GetStream, request);
}
private void GetStream(IAsyncResult result)
{
try
{
HttpWebRequest request1 = (HttpWebRequest)result.AsyncState;
using (Stream postStream = request1.EndGetRequestStream(result))
{
int len = postBody.Length;
len += mainBody.Length;
len += endBody.Length;
byte[] postArray = new byte[len + 1];
Encoding.UTF8.GetBytes(postBody.ToString()).CopyTo(postArray, 0);
Encoding.UTF8.GetBytes(mainBody).CopyTo(postArray, postBody.Length);
Encoding.UTF8.GetBytes(endBody).CopyTo(postArray, postBody.Length + mainBody.Length);
postStream.Write(postArray, 0, postArray.Length);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望它会缓冲所有内容,直到您完成写入为止,此时它将能够立即使用内容长度。如果您设置:
那么我怀疑至少当您写入流时它会失败。
顺便说一句,您对
postArray
所需长度的计算似乎假设每个字符一个字节,但情况并非总是如此......并且您正在调用ToString
在postBody
上,看起来像是多余的。我不确定为什么你要尝试在一次调用中写入...或者你可以调用 Write 三次:或者(最好)只使用 < code>StreamWriter:
也不清楚为什么在初始化
postArray
时要在所需长度上加 1。您是否尝试在数据末尾发送额外的“0”字节?I expect it's buffering everything until you're done writing, at which point it will be able to use the content length immediately. If you set:
then I suspect it will fail at least when you write to the stream.
Btw, your calculation of the required length for
postArray
appears to be assuming one byte per character, which won't always be the case... and you're callingToString
onpostBody
which looks like it's redundant. I'm not sure why you're trying to write in a single call anyway... either you could call Write three times:or (preferrably) just use a
StreamWriter
:It's also unclear why you've added 1 to the required length when initializing
postArray
. Are you trying to send an extra "0" byte at the end of the data?