[英]Creating 2 http requests on the same connection
I would like to create 2 HTTP requests on the same connection (HTTP persistent connection). 我想在同一个连接上创建2个HTTP请求(HTTP持久连接)。
I'm using HttpWebRequest: 我正在使用HttpWebRequest:
WebRequest request = HttpWebRequest.Create("http://localhost:14890/Service1/3");
WebResponse response = request.GetResponse();
byte[] buffer = new byte[1024];
int x = response.GetResponseStream().Read(buffer, 0, 1024);
string str = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
I think if I use request
again it will create a whole new HTTP connection which I don't want to do. 我认为如果我再次使用
request
,它将创建一个我不想做的全新HTTP连接。
Is there another class I can use isntead or is there something I'm missing? 是否有另一个我可以使用的课程,或者是否有我缺少的东西?
I'm also not sure how the WebClient class works with respect to persistent connections. 我也不确定WebClient类相对于持久连接如何工作。
Set the KeepAlive
property . 设置
KeepAlive
属性 。
For example: 例如:
string str;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:14890/Service1/3");
request.KeepAlive = true;
using (WebResponse response = request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream, Encoding.ASCII)) {
str = reader.ReadToEnd();
}
if you want to keep your server session from one httprequest to the next, you have explicitly store and send your session identifier, unlike with msinet.ocx which does it all for you. 如果你想将你的服务器会话从一个httprequest保持到下一个,你已经明确地存储并发送你的会话标识符,这与msinet.ocx不同,后者为你做了一切。 for instance when connecting to a php webserver, the session identifier is stored in a header labelled Set-Cookie/PHPSESSID=... and this has header has to be manually added to the next httprequest but renamed to Cookie/PHPSESSID=....
例如,当连接到php Web服务器时,会话标识符存储在标有Set-Cookie / PHPSESSID = ...的标头中,此标头必须手动添加到下一个httprequest中,但重命名为Cookie / PHPSESSID = ... 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.