对于使用httpwebrequest的post请求,当我写入请求流时,在什么时候发送数据?是关闭请求流还是调用getResponse?需要getResponse调用吗?
.NET文档似乎不太清楚到底发生了什么
下面是我很好奇的代码:
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentLength = jsonData.Length;
request.ContentType = "application/json";
Stream requestStream = request.GetRequestStream();
requestStream.Write(jsonData, 0, jsonData.Length);
requestStream.Close();
var response = request.GetResponse() as HttpWebResponse;
谢谢!
最佳答案
是的,GetResponse
调用是必须的,不仅对于post请求,对于get,head请求也是如此。请求/数据在调用GetResponse
时发送。