开发者

Creating 2 http requests on the same connection

I would like to create 2 HTTP requests on the same connection (HTTP persistent connection).

I'm using HttpWebRequest:

        WebRequest request = HttpWebRequest.Create("http://localhost:14890/Service1开发者_如何学JAVA/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.

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.


Set the KeepAlive property.

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. 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=....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜