简体   繁体   English

在同一连接上创建2个http请求

[英]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.

相关问题 处理具有相同内容的HTTP请求 - Handling HTTP requests with same content 如何跨多个请求重用HTTP连接? - How to reuse HTTP connection across multiple requests? AgilityPack-维护多个HTTP请求的“连接” - AgilityPack - Maintaining a 'connection' for several HTTP Requests 使用相同连接的多线程Web请求 - Multi Threaded web requests using same connection 对同一个http主机的多个同时请求 - Multiple simultaneous requests to the same http host C# - FtpWebRequest - 通过同一连接/登录的多个请求 - C# - FtpWebRequest - Multiple requests over the same connection/login ServiceStack / FluentNHibernate / MySQL-两个并发请求使用相同的连接 - ServiceStack / FluentNHibernate / MySQL - Same connection used by two concurrent requests 多个请求同时出现连接字符串错误 - Connection string error when multiple requests at the same time 多个请求同时发生意外连接状态 - Unexpected connection state when multiple requests at the same time 在这些条件下是否真的可以同时发送多个 http 请求 - Is it really possible to send multiple http requests at the same time with these conditions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM