简体   繁体   中英

Using login cookie in another website c#

I'm trying to get cookies form a loginform, saved it on my cookie container cookieJar, and using in the next request. The cookies are saved correctly(at least,the count shows an appropiate quantity,but when doing the webrequest3, I not getting the content , getting the page as not logged in.

PD: I read the related posts, but the major are not completly implemented(obviously),and the others are doing exactly as I do, so I'm in a loss.

 CookieContainer cookieJar = new CookieContainer();
        //The First Req

        HttpWebRequest webRequest1 = (HttpWebRequest)WebRequest.Create("url1");
        webRequest1.Method = "GET";
        webRequest1.ContentType = "text/html";
        webRequest1.KeepAlive = true;
        webRequest1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36";
        webRequest1.Host = "url1";
        webRequest1.CookieContainer = cookieJar;
        webRequest1.ContentType = "text/html";
        HttpWebResponse webResponse;
        webResponse = (HttpWebResponse)webRequest1.GetResponse();
        Console.WriteLine(cookieJar.Count.ToString());
        StreamReader reader = new StreamReader(webResponse.GetResponseStream());

        // Read the content fully up to the end.
        string responsereq = reader.ReadToEnd();

        // Clean up the streams.
        reader.Close();
        webResponse.Close();
        Console.ReadKey();

        //Second Request
        HttpWebRequest webRequest3 = (HttpWebRequest)WebRequest.Create("url2");
        webRequest3.Method = "GET";
        webRequest3.KeepAlive = true;
        webRequest3.UserAgent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36";
        webRequest3.Host = "url2";
        webRequest3.CookieContainer = cookieJar;
        webRequest3.ContentType = "text/html";


        Console.WriteLine(cookieJar.Count.ToString() +"CookieJar");
        Console.ReadKey();
        webResponse = (HttpWebResponse)webRequest3.GetResponse();
        StreamReader reader3 = new StreamReader(webResponse.GetResponseStream());

        // Read the content fully up to the end.
        string responseFromServer = reader3.ReadToEnd();
        Console.WriteLine(responseFromServer);

      // Clean up the streams.
      webResponse.Close();

Console.ReadKey();

EDIT:

I get with fiddler that when loggin from the explorer,enter to a page automatically after the webrequest1, don't save any cookie,but seems to use some server-side check, that if you don't enter to that page before the webrequest3, the webrequest2 didn't recognize your login. So, creating another webrequest before webrequest3, do the trick.

Try with following.

  1. First get cookies from the response of first request.

     HttpWebResponse webResponse; webResponse = (HttpWebResponse)webRequest1.GetResponse(); 

    CookieContainer cookieJar= new CookieContainer();

      foreach (Cookie cook in webResponse .Cookies) { cookieJar.Add(cook); } 
  2. Pass it to subsequent request.

    webRequest3.CookieContainer = cookieJar;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM