Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

c# - Multiple WebRequest in same session

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...