如何使用Cookie与HttpWebRequest的 [英] how to use cookies with HttpWebRequest

查看:252
本文介绍了如何使用Cookie与HttpWebRequest的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立用于从所述网页数据检索应用程序。该网页是密码保护,并创建cookie中的用户登录时。

为了检索应用程序第一次有记录的数据:使用户名和密码的网络请求和存储的cookie。然后,当cookie被存储它必须被加入到的所有请求的报头

下面是发出请求并检索内容的方法:

 公共无效getAsyncDailyPDPContextActivati​​onDeactivati​​on()
    {
        HttpWebRequest的HttpWebRequest的=(HttpWebRequest的)WebRequest.Create(dailyPDPContextActivati​​onDeactivati​​on);

        IAsyncResult的asyncResult = httpWebRequest.BeginGetResponse(NULL,NULL);

        asyncResult.AsyncWaitHandle.WaitOne();

        使用(HttpWebResponse httpWebResponse =(HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult))
        使用(StreamReader的responseStreamReader =新的StreamReader(httpWebResponse.GetResponseStream()))
        {
            字符串的responseText = responseStreamReader.ReadToEnd();
        }

    }
 

有谁知道如何才能加入一个cookie到页眉修改此方法?

下面是请求的报头结构与浏览器发出的打印屏幕:

我会还感谢,如果有人提出了一个方法,从响应存储的cookie(当应用程序发出请求的HTTP:xxx.xxx.xxx/login用户名= XXX和放大器;密码= XXX是创建的cookie,并有被储存以供将来的请求)。

解决方案

 的CookieContainer的CookieContainer =新的CookieContainer();
HttpWebRequest的HttpWebRequest的=(HttpWebRequest的)WebRequest.Create(...);
httpWebRequest.CookieContainer =的CookieContainer;
 

然后你再用这种的CookieContainer在后续请求:

  HttpWebRequest的httpWebRequest2 =(HttpWebRequest的)WebRequest.Create(...);
httpWebRequest2.CookieContainer =的CookieContainer;
 

I am creating an application for data retrieval from the web page. The page is password protected and when the user logs in the cookie is created.

In order to retrieve the data the application first has to log in: make web request with username and password and store the cookie. Then when the cookie is stored it has to be added into the headers of all requests.

here is the method which makes requests and retrieves the content:

public void getAsyncDailyPDPContextActivationDeactivation()
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(dailyPDPContextActivationDeactivation);

        IAsyncResult asyncResult = httpWebRequest.BeginGetResponse(null, null);

        asyncResult.AsyncWaitHandle.WaitOne();

        using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult))
        using (StreamReader responseStreamReader = new StreamReader(httpWebResponse.GetResponseStream()))
        {
            string responseText = responseStreamReader.ReadToEnd();
        }

    }

Does anyone know how to modify this method in order to add a cookie into the header?

Below is the print screen of the header structure of the request made with browser:

I would be also thankful if anyone suggested a way to store cookie from the response (when the application makes a request http:xxx.xxx.xxx/login?username=xxx&password=xxx the cookie is created and has to be stored for future requests).

解决方案

CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(...);
httpWebRequest.CookieContainer = cookieContainer;

Then you reuse this CookieContainer in subsequent requests:

HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(...);
httpWebRequest2.CookieContainer = cookieContainer;

这篇关于如何使用Cookie与HttpWebRequest的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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