Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
    }
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
135 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...