0

我尝试在此站点上提交表单:freeclassifieds,使用支持 cookie 的 WebClient。我升级的 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);
        }
    }
}

但是当我尝试提交表单时,我只是被阻止并接收登录页面作为响应。我检查了登录页面中的 cookie 和字段,一切似乎都已调整好。为什么登录成功后收不到正确的页面?

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);
    }
4

1 回答 1

0

如果/如果配置正确,则不能“欺骗” ASP.Net Web 表单Postback- MSDN 参考:VIEWSTATEEVENTVALIDATION

于 2012-12-17T00:31:36.847 回答