Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple WebRequest in same session

Tags:

html

c#

asp.net

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.

like image 307
Ankit Avatar asked Apr 24 '09 23:04

Ankit


1 Answers

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
        }
    }
like image 174
Erv Walter Avatar answered Oct 07 '22 20:10

Erv Walter