在同一会话多的WebRequest [英] Multiple WebRequest in same session

查看:108
本文介绍了在同一会话多的WebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个html页面从而节省网页(与图像)的功能。
我使用的HttpWebRequest请求的网页的内容。
我的功能看起来像

I am trying to write a function which saves a webpage (with its images) as a html page. I am using HttpWebRequest to request for the content of webpages. My function looks something like

void SaveUrl(string sourceURL, string savepath)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(sourceURL);
    HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
    StreamReader responseReader = new StreamReader(response.GetResponseStream());

    string sResponseHTML = responseReader.ReadToEnd();
    using (StreamWriter sw = new StreamWriter(savepath, false))
    {
        sw.Write(sResponseHTML);
    }

    string[] ImageUrl = GetImgLinks(sResponseHTML);
    foreach (string imagelink in ImageUrl)
    {
        HttpWebRequest imgRequest = (HttpWebRequest)WebRequest.Create(imagelink);
        HttpWebResponse imgresponse = (HttpWebResponse)imgRequest.GetResponse();
        //Code to save image
    }
}

我的问题在这里我要让所有的WebRequest在同一会话和不想创建每个imgRequest一个新的会话,因为许多在我的网页图像的动态生成和临时存储。所以如果我在做同一个会话的请求这些图像只能是牵强。

My problem here is I want to make all the webrequest in same session and dont want to create a new session with each imgRequest, as many of the images on my webpage are dynamically generated and are stored temporarily. so those images can only be fetched if I make a request in same session.

推荐答案

会议通常使用Cookie的工作模式。如果你希望所有的请求是同一个会话的一部分,你需要坚持请求之间的cookie。您可以通过创建一个的CookieContainer,并将其提供给每个HttpWebRequest的对象做到这一点。

Sessions generally work by using cookies. If you want all your requests to be part of the same session, you need to persist the cookies between requests. You do this by creating a CookieContainer and providing it to each of the HttpWebRequest objects.

这是你的code更新为使用的CookieContainer:

Here's your code updated to use a CookieContainer:

    void SaveUrl(string sourceURL, string savepath) {
        CookieContainer cookies = new CookieContainer();
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(sourceURL);
        webRequest.CookieContainer = cookies;

        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
        StreamReader responseReader = new StreamReader(response.GetResponseStream());

        string sResponseHTML = responseReader.ReadToEnd();
        using (StreamWriter sw = new StreamWriter(savepath, false)) {
            sw.Write(sResponseHTML);
        }

        string[] ImageUrl = GetImgLinks(sResponseHTML);
        foreach (string imagelink in ImageUrl) {
            HttpWebRequest imgRequest = (HttpWebRequest)WebRequest.Create(imagelink);
            imgRequest.CookieContainer = cookies;
            HttpWebResponse imgresponse = (HttpWebResponse)imgRequest.GetResponse();
            //Code to save image
        }
    }

这篇关于在同一会话多的WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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