HttpWebRequest.GetRequestStream:它是做什么的? [英] HttpWebRequest.GetRequestStream : What it does?

查看:612
本文介绍了HttpWebRequest.GetRequestStream:它是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码示例:

HttpWebRequest request =
   (HttpWebRequest)HttpWebRequest.Create("http://some.existing.url");

request.Method = "POST";
request.ContentType = "text/xml";

Byte[] documentBytes = GetDocumentBytes ();


using (Stream requestStream = request.GetRequestStream())
{
   requestStream.Write(documentBytes, 0, documentBytes.Length);
   requestStream.Flush();
   requestStream.Close();
}

当我执行request.GetRequestStream ()时,没有任何要发送的请求.从方法的名称及其显示的intellisense(获取要用于写入请求数据的System.IO.Stream")来看,没有任何内容表明此代码行将连接到远程服务器.
但似乎确实如此...

When I do request.GetRequestStream (), there's nothing to send in the request. From the name of the method, and the intellisense it shows ("Get System.IO.Stream to use to write request data"), nothing indicates that this line of code will connect to the distant server.
But it seems it does...

有人可以向我解释HttpWebRequest.GetRequestStream ()的确切含义吗?

Can anyone explain to me what HttpWebRequest.GetRequestStream () exactly does ?

感谢您的启发.

推荐答案

获取请求流不会触发发布,但是关闭流会触发.发布数据通过以下方式发送到服务器:

Getting the request stream does not trigger the post, but closing the stream does. Post data is sent to the server in the following way:

  1. 已打开与主机的连接
  2. 发送请求和标头
  3. 写帖子数据
  4. 等待响应.

刷新和关闭流的操作是最后一步,一旦输入流关闭(即客户端已将其需要的内容发送到服务器),服务器就可以返回响应.

The act of flushing and closing the stream is the final step, and once the input stream is closed (i.e. the client has sent what it needs to the server), then the server can return a response.

这篇关于HttpWebRequest.GetRequestStream:它是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆