2

I have a webbrowser control which loads a page. I then hit a button to call this method:

public void get(Uri myUri)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
        CookieContainer cookieJar = new CookieContainer();
        cookieJar.SetCookies(webBrowser1.Document.Url,webBrowser1.Document.Cookie.Replace(';', ','));
        request.CookieContainer = cookieJar;


        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        int cookieCount = cookieJar.Count;

        Stream receiveStream = response.GetResponseStream();
        StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

        txt.Text = readStream.ReadToEnd();


            txt2.Text = cookieCount.ToString();


    }

As from the cookieCount int i can see that if i call the method before logging in on the page in the web browser control i would get 6 cookies, and after i log in i get 7. However, even with the cookies the response i get is the same as if i wouldnt have been logged in. So i am guessing that the cookies isnt being sent with the request?

Thanks!

user2725580
  • 1,293
  • 2
  • 17
  • 21

2 Answers2

1

You're recreating your CookieContainer every time you call this method, you need to use the same CookieContainer in all requests

you can use this code, to handle your requests:

      static CookieContainer cookies = new CookieContainer();

        static HttpWebRequest GetNewRequest(string targetUrl, CookieContainer SessionCookieContainer)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);
            request.CookieContainer = SessionCookieContainer;
            request.AllowAutoRedirect = false;
            return request;
        }

        public static HttpWebResponse MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary<string, string> parameters = null)
        {


            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Accept: */*";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.CookieContainer = SessionCookieContainer;
            request.AllowAutoRedirect = false;

            if (parameters != null)
            {
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                string postData = "";
                foreach (KeyValuePair<String, String> parametro in parameters)
                {
                    if (postData.Length == 0)
                    {
                        postData += String.Format("{0}={1}", parametro.Key, parametro.Value);
                    }
                    else
                    {
                        postData += String.Format("&{0}={1}", parametro.Key, parametro.Value);
                    }

                }
                byte[] postBuffer = UTF8Encoding.UTF8.GetBytes(postData);
                using (Stream postStream = request.GetRequestStream())
                {
                    postStream.Write(postBuffer, 0, postBuffer.Length);
                }
            }
            else
            {
                request.Method = "GET";
            }


            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            SessionCookieContainer.Add(response.Cookies);


            while (response.StatusCode == HttpStatusCode.Found)
            {
                response.Close();
                request = GetNewRequest(response.Headers["Location"], SessionCookieContainer);
                response = (HttpWebResponse)request.GetResponse();
                SessionCookieContainer.Add(response.Cookies);
            }


            return response;
        }

and to request a page,

 HttpWebRequest request = GetNewRequest("http://www.elitepvpers.com/forum/login.php?do=login", cookies);
 Dictionary<string,string> parameters = new Dictionary<string,string>{{"your params","as key value"};


            HttpWebResponse response = MakeRequest(request, cookies, parameters);
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                if(!reader.EndOfStream)
                {
                    Console.Write(reader.ReadToEnd());
                }
            }
  • Thanks! I cant seem to get the syntax for the code to request a page right. would you wrap in in a method or something please? I am pretty new to c#. – user2725580 Nov 22 '13 at 01:54
  • here you can see a sample application that do this requests http://stackoverflow.com/questions/11601621/httpwebrequest-another-page/11601883#11601883 – Victor Ribeiro da Silva Eloy Nov 22 '13 at 01:57
1

When matching the session, your web server may be taking into account some other HTTP request headers, besides cookies. To name a few: User-Agent, Authorization, Accept-Language.

Because WebBrowser control and WebRequest do not share sessions, you'd need to replicate all headers from the WebBrowser session. This would be hard thing to do, as you'd need to intercept WebBrowser trafic, in a way similar to what Fiddler does.

A more feasible solution might be to stay on the same session with WebBrowser by using Windows UrlMon APIs like URLOpenStream, URLDownloadToFile etc., instead of WebRequest. That works, because WebBrowser uses UrlMon library behind the scene.

I've recently answered some related questions:

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486