Website authentication issues from code

I am trying to write code that will authenticate to the wallbase.cc website. I looked at what it does using the Firfebug / Chrome Developer tools, and it looks pretty easy:

The message "usrname = $ USER & pass = $ PASS & nopass_email = Enter + in + your + email address + and + press + enter & nopass = 0" on the web page "http://wallbase.cc/user/login ", save the returned cookies and use them in all future requests.

Here is my code:

    private CookieContainer _cookies = new CookieContainer();

    //......

    HttpPost("http://wallbase.cc/user/login", string.Format("usrname={0}&pass={1}&nopass_email=Type+in+your+e-mail+and+press+enter&nopass=0", Username, assword));

    //......


    private string HttpPost(string url, string parameters)
    {
        try
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            //Add these, as we're doing a POST
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";

            ((HttpWebRequest)req).Referer = "http://wallbase.cc/home/";
            ((HttpWebRequest)req).CookieContainer = _cookies;

            //We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameters);
            req.ContentLength = bytes.Length;
            System.IO.Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length); //Push it out there
            os.Close();

            //get response
            using (System.Net.WebResponse resp = req.GetResponse())
            {

                if (resp == null) return null;
                using (Stream st = resp.GetResponseStream())
                {
                    System.IO.StreamReader sr = new System.IO.StreamReader(st);
                    return sr.ReadToEnd().Trim();
                }
            }
        }
        catch (Exception)
        {
            return null;
        }
    }

HttpPost , ( /). cookie cookie, - . cookie cookie, , , , cookie , , .

, Python : https://github.com/sevensins/Wallbase-Downloader/blob/master/wallbase.sh ( 336)

, ?

# 1
"/" , "/" "/" . , , , , , .

# 2

.NET 3.5. .NET 4, System.Net.ServicePointManager.Expect100Continue = false ( , ), , . , -, - , .NET.

+5
3

, , , stackoverflow.

Cookie aware WebClient, HTML 1.0.

public class CookieAwareWebClient : WebClient
{
    private CookieContainer cookie = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        request.ProtocolVersion = HttpVersion.Version10;
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = cookie;
        }
        return request;
    }
}

, Authentication, , , response.

var client = new CookieAwareWebClient();
client.UseDefaultCredentials = true;
client.BaseAddress = @"http://wallbase.cc";

var loginData = new NameValueCollection();
loginData.Add("usrname", "test");
loginData.Add("pass", "123");
loginData.Add("nopass_email", "Type in your e-mail and press enter");
loginData.Add("nopass", "0");
var result = client.UploadValues(@"http://wallbase.cc/user/login", "POST", loginData);

string response = System.Text.Encoding.UTF8.GetString(result);

, HTML Visualizer Visual Studio, , , authenticate , authenticated.

Success

- CookieContainer HTTP 1.0 1.1. , 1.0 , . fooobar.com/questions/33261/...

Fiddler, , , C# Client, , - Chrome. C# Client. , HTML 1.0 HTTP/1.0 302 Found, , . HTML 1.1, HTTP/1.1 417 Expectation Failed message. Fiddler

stackoverflow . HTTP POST : 417 " ."

: Hack/Fix .NET 3.5

, 3.5 4.0, . , 3.5 cookie , , , - .

WebClient . http://dot-net-expertise.blogspot.fr/2009/10/cookiecontainer-domain-handling-bug-fix.html

public class CookieAwareWebClient : WebClient
{
    public CookieContainer cookies = new CookieContainer();
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        var httpRequest = request as HttpWebRequest;
        if (httpRequest != null)
        {
            httpRequest.ProtocolVersion = HttpVersion.Version10;
            httpRequest.CookieContainer = cookies;

            var table = (Hashtable)cookies.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cookies, new object[] { });
            var keys = new ArrayList(table.Keys);
            foreach (var key in keys)
            {
                var newKey = (key as string).Substring(1);
                table[newKey] = table[key];
            }
        }
        return request;
    }
}

var client = new CookieAwareWebClient();

var loginData = new NameValueCollection();
loginData.Add("usrname", "test");
loginData.Add("pass", "123");
loginData.Add("nopass_email", "Type in your e-mail and press enter");
loginData.Add("nopass", "0");

// Hack: Authenticate the user twice!
client.UploadValues(@"http://wallbase.cc/user/login", "POST", loginData);
var result = client.UploadValues(@"http://wallbase.cc/user/login", "POST", loginData);

string response = System.Text.Encoding.UTF8.GetString(result);
+6

:

//get response
using (System.Net.WebResponse resp = req.GetResponse())
{
    foreach (Cookie c in resp.Cookies)
        _cookies.Add(c);
    // Do other stuff with response....
}

, , - 302 (), - .Net , cookie, . :

req.AllowAutoRedirect = false;
+3

The Python link you are using uses a different referrer (http://wallbase.cc/start/). It is also followed by another message (http://wallbase.cc/user/adult_confirm/1). Try a different referrer and follow up with this POST.

I think you are authenticated correctly, but you need more information / statements from you before proceeding.

+2
source

All Articles