不能登录到asp.net网站与Web客户端 [英] Can't login into asp.net website with WebClient

查看:87
本文介绍了不能登录到asp.net网站与Web客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图向本网站的形式: freeclassifieds ,使用支持cookie的Web客户端。
我升级的Web客户端:

 公共类CookieWebClient:Web客户端
{
    私人只读的CookieContainer集装箱=新的CookieContainer();    公共CookieWebClient(容器的CookieContainer)
    {
        this.container =容器;
    }    保护覆盖的WebRequest GetWebRequest(URI地址)
    {
        WebRequest的解析度= base.GetWebRequest(地址);
        VAR请求=资源为HttpWebRequest的;
        如果(要求!= NULL)
        {
            request.CookieContainer =容器;
        }
        返回水库;
    }    保护覆盖WebResponse类GetWebResponse(WebRequest的要求,IAsyncResult的结果)
    {
        WebResponse的响应= base.GetWebResponse(请求,结果);
        ReadCookies(响应);
        返回响应;
    }    保护覆盖WebResponse类GetWebResponse(WebRequest的要求)
    {
        WebResponse的响应= base.GetWebResponse(请求);
        ReadCookies(响应);
        返回响应;
    }    私人无效ReadCookies(WebResponse的R)
    {
        VAR响应= R为HttpWebResponse;
        如果(响应!= NULL)
        {
            CookieCollection饼干= response.Cookies;
            container.Add(饼干);
        }
    }
}

但是,当我试图提交表单,我只是得到阻止,收到登录页面作为响应。
我检查了饼干和登录页面的领域,似乎一切都进行调整的。
为什么我不能收到成功登录后正确的页面?

 静态无效的主要(字串[] args)
    {
        字符串LINK1 =htt​​p://www.freeclassifieds.com/logon.aspx;        字符串文件名= @D:\\ Dropbox的\\ MyProjects下\\ AutoPostMachine \\ myHtml.html        CookieWebClient WC =新CookieWebClient(新的CookieContainer());        NameValueCollection中postDataCollection =新的NameValueCollection();
        postDataCollection.Add(ctl00 $ phMain $ txtEmail,myEmail);
        postDataCollection.Add(ctl00 $ phMain $ txtPassword,MYPASSWORD);
        postDataCollection.Add(ctl00 $ phMain $ btnLogin,登录);        wc.Encoding = Encoding.UTF8;
        wc.Credentials = CredentialCache.DefaultCredentials;
        wc.Headers [Htt的prequestHeader.ContentType] =应用/的X WWW的形式urlen codeD;
        wc.Headers [Htt的prequestHeader.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 =(直接)| utmccn =(直接)| utmcmd =(无);        字节[] = byteArr wc.UploadValues​​(LINK1,postDataCollection);
        的FileStream FS =新的FileStream(文件名,FileMode.Create);
        fs.Write(byteArr,0,byteArr.Length);        的Process.Start(文件名);
    }


解决方案

如果/当配置正确,你不能欺骗一个ASP.Net Web窗体回发 - MSDN参考: VIEWSTATE EVENTVALIDATION

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

解决方案

If/When configured properly, you cannot "spoof" an ASP.Net Web Forms Postback - MSDN ref: VIEWSTATE and EVENTVALIDATION

这篇关于不能登录到asp.net网站与Web客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆