views:

244

answers:

2

Hello,

Language: C# Development Environment: Visual Studio 2008

Sorry if the english is not perfect.

I want to login to a Website and get some Data from there. My Problem is that the Cookies does not work. Everytime the Website says that I should activate Cookies but i activated the Cookies trough a Cookiecontainer.

I sniffed the traffic serveral times for the login progress and I see no problem there. I tried different methods to login and I have searched if someone else have this Problem but no results...

Login Page is: "www.uploaded.to", Here is my Code to Login in Short Form:

        private void login()
        {
        //Global CookieContainer for all the Cookies
        CookieContainer _cookieContainer = new CookieContainer();

        //First Login to the Website
        HttpWebRequest _request1 = (HttpWebRequest)WebRequest.Create("http://uploaded.to/login");
        _request1.Method = "POST";
        _request1.CookieContainer = _cookieContainer;

        string _postData = "email=XXXXX&password=XXXXX";
        byte[] _byteArray = Encoding.UTF8.GetBytes(_postData);

        Stream _reqStream = _request1.GetRequestStream();

        _reqStream.Write(_byteArray, 0, _byteArray.Length);
        _reqStream.Close();

        HttpWebResponse _response1 = (HttpWebResponse)_request1.GetResponse();
        _response1.Close();


        //########################

        //Follow the Link from Request1
        HttpWebRequest _request2 = (HttpWebRequest)WebRequest.Create("http://uploaded.to/login?coo=1");
        _request2.Method = "GET";
        _request2.CookieContainer = _cookieContainer;

        HttpWebResponse _response2 = (HttpWebResponse)_request2.GetResponse();
        _response2.Close();


        //#######################

        //Get the Data from the Page after Login
        HttpWebRequest _request3 = (HttpWebRequest)WebRequest.Create("http://uploaded.to/home");
        _request3.Method = "GET";
        _request3.CookieContainer = _cookieContainer;

        HttpWebResponse _response3 = (HttpWebResponse)_request3.GetResponse();
        _response3.Close();

        }

I'm stuck at this problem since many weeks and i found no solution that works, please help...

A: 

Why not use WebClient class?

NameValueCollection loginData = new NameValueCollection();
loginData.Add("email", "your_email");
loginData.Add("password", "your_password");

WebClient client = new WebClient();
string source = Encoding.UTF8.GetString(client.UploadValues("http://uploaded.to/login", loginData));
string cookie = client.ResponseHeaders["Set-Cookie"];
client.Headers["Cookie"] = cookie;

source = client.DownloadString("http://uploaded.to/some-page");
Mika Kolari
Its the same. If I try your code the programm does not login, the only cookie that i receive is the language cookie and no Authentication Cookie. If I change the code a little bit then I have again the problem that the website say that I have no cookies activated...A login to a website ist normally very easy trough a program but at this website ists very hard and I dont know where the problem is.
Collin Peters
A: 

I've tried it again, the first request work (login) now. I get a cookie with a Authentification. The response text is the url: http://uploaded.to/login?coo=1 now I must make a GET request on this url BUT the problem is, it sends no cookies at this request, checked with Wireshark. Have the httpwebrequest problems with cookies?

My Code:

CookieContainer _cookieContainer = new CookieContainer();

        //First Login to the Website
        HttpWebRequest _request1 = (HttpWebRequest)WebRequest.Create("http://uploaded.to/login");
        _request1.Method = "POST";
        _request1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        _request1.AllowAutoRedirect = false;
        _request1.CookieContainer = _cookieContainer;
        _request1.ContentType = "application/x-www-form-urlencoded";
        _request1.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)";
        _request1.Headers.Add(HttpRequestHeader.CacheControl, "no-cache=set-cookie");

        string _postData = "email=XXXXX&password=XXXXXX";
        byte[] _byteArray = Encoding.UTF8.GetBytes(_postData);
        _request1.ContentLength = _byteArray.Length;

        Stream _reqStream = _request1.GetRequestStream();
        _reqStream.Write(_byteArray, 0, _byteArray.Length);
        _reqStream.Close();



        HttpWebResponse _response1 = (HttpWebResponse)_request1.GetResponse();
        StreamReader _reader1 = new StreamReader(_response1.GetResponseStream());
        CookieCollection _cookieCollection = new CookieCollection();
        foreach (Cookie _cook in _response1.Cookies)
        {
            _cookieCollection.Add(_cook);
        }
        wbrowser_html.DocumentText = _reader1.ReadToEnd();

        _response1.Close();
        _reader1.Close();

        //########################

        HttpWebRequest _request2 = (HttpWebRequest)WebRequest.Create("http://uploaded.to/login?coo=1");
        _request2.Method = "GET";
        _request2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        _request2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)";
        _request2.Referer = "http://uploaded.to/login";
        _request2.KeepAlive = true;

        //Here I set the language and the auth cookie, works fine
        _request2.CookieContainer = _cookieContainer;
        _request2.CookieContainer.Add(_cookieCollection);
        _request2.Headers.Add(HttpRequestHeader.CacheControl, "no-cache=set-cookie");

        HttpWebResponse _response2 = (HttpWebResponse)_request2.GetResponse();
        StreamReader _reader2 = new StreamReader(_response2.GetResponseStream());
        wbrowser_html.DocumentText = _reader2.ReadToEnd();

        _response2.Close();
        _reader2.Close();

And now if I check with Wireshark and see there are no cookies in the second request?! I've add them to the request but anyway they were not send to the server??

Have anyone an idea to fix this problem? Its really important...

Collin Peters