简体   繁体   中英

Can't login into asp.net website with WebClient

I tried to submit form on this site: freeclassifieds , using WebClient that support cookies. My upgraded WebClient:

public class CookieWebClient : WebClient
{
    private readonly CookieContainer container = new CookieContainer();

    public CookieWebClient(CookieContainer container)
    {
        this.container = container;
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest res = base.GetWebRequest(address);
        var request = res as HttpWebRequest;
        if (request != null)
        {
            request.CookieContainer = container;
        }
        return res;
    }

    protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
    {
        WebResponse response = base.GetWebResponse(request, result);
        ReadCookies(response);
        return response;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        ReadCookies(response);
        return response;
    }

    private void ReadCookies(WebResponse r)
    {
        var response = r as HttpWebResponse;
        if (response != null)
        {
            CookieCollection cookies = response.Cookies;
            container.Add(cookies);
        }
    }
}

But when I'm trying to submit form, I just get blocked and receiving the login page as a response. I checked the cookies and the fields in the login page, everything seems to be tuned up. Why can't I receive the right page after success login?

static void Main(string[] args)
    {
        string link1 = "http://www.freeclassifieds.com/logon.aspx";

        string fileName= @"D:\Dropbox\myProjects\AutoPostMachine\myHtml.html";

        CookieWebClient wc = new CookieWebClient(new CookieContainer());

        NameValueCollection postDataCollection= new NameValueCollection();
        postDataCollection.Add("ctl00$phMain$txtEmail", "myEmail");
        postDataCollection.Add("ctl00$phMain$txtPassword","myPassword");
        postDataCollection.Add("ctl00$phMain$btnLogin","Login");

        wc.Encoding = Encoding.UTF8;
        wc.Credentials = CredentialCache.DefaultCredentials;
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        wc.Headers[HttpRequestHeader.Cookie] = "ASP.NET_SessionId=mfqdx1505nnbw3bxws01so12; __utma=19755559.732186057.1355692154.1355692154.1355695835.2; __utmb=19755559.3.10.1355695835; __utmc=19755559; __utmz=19755559.1355692154.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)";

        byte[] byteArr= wc.UploadValues(link1, postDataCollection);
        FileStream fs = new FileStream(fileName, FileMode.Create);
        fs.Write(byteArr, 0, byteArr.Length);

        Process.Start(fileName);
    }

如果/配置正确,则不能“欺骗” ASP.Net Web窗体Postback VIEWSTATE参考: VIEWSTATEEVENTVALIDATION

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