即使没有互联网连接,HttpWebRequest 也会获取流

发布于 2024-10-31 23:26:31 字数 1397 浏览 0 评论 0原文

我正在使用下面的代码将字节数组发送到站点。为什么即使没有互联网连接,此代码也不会抛出异常?即使没有连接,我也能够获取流并能够写入它。我希望它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窗影残 2024-11-07 23:26:31

我希望它会缓冲所有内容,直到您完成写入为止,此时它将能够立即使用内容长度。如果您设置:

request.AllowWriteStreamBuffering = false;

那么我怀疑至少当您写入流时它会失败。

顺便说一句,您对 postArray 所需长度的计算似乎假设每个字符一个字节,但情况并非总是如此......并且您正在调用 ToStringpostBody 上,看起来像是多余的。我不确定为什么你要尝试在一次调用中写入...或者你可以调用 Write 三次:

byte[] postBodyBytes = Encoding.UTF8.GetBytes(postBody);
postStream.Write(postBodyBytes, 0, postBodyBytes.Length);
// etc

或者(最好)只使用 < code>StreamWriter:

using (Stream postStream = request1.EndGetRequestStream(result))
{
    using (StreamWriter writer = new StreamWriter(postStream)
    {
        writer.Write(postBody);
        writer.Write(mainBody);
        writer.Write(endBody);
    }
}

不清楚为什么在初始化 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:

request.AllowWriteStreamBuffering = false;

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 calling ToString on postBody 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:

byte[] postBodyBytes = Encoding.UTF8.GetBytes(postBody);
postStream.Write(postBodyBytes, 0, postBodyBytes.Length);
// etc

or (preferrably) just use a StreamWriter:

using (Stream postStream = request1.EndGetRequestStream(result))
{
    using (StreamWriter writer = new StreamWriter(postStream)
    {
        writer.Write(postBody);
        writer.Write(mainBody);
        writer.Write(endBody);
    }
}

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文