2 q900907 q900907 于 2013.10.16 22:43 提问

C# 模拟post登陆后,关键cookie发送不出去,求帮助

想做一个登陆小米网站的程序,在使用post模拟登陆的时候,能够获取到登陆后的cookie,cookieContainer里面有3个域名。(.xiaomi.com)(account.xiaomi.com)(.account.xiaomi.com) 在.account.xiaomi.com中存在着关键的cookie,pwdToken和passToken都有存在着。但是使用这个cookieContainer,然后httpWebRequest的时候,用get方法获取https://account.xiaomi.com/pass/userInfo?userId=73896043 的时候,我用fiddler发现他只发送了account.xiaomi.com域名中的cookie,而没有发送.account.xiaomi.com中的cookie。导致进页面被认为没登陆,一直返回登陆页面。第一次做winform的程序,被这个问题困扰了好多天了,一直没能解决。
使用ie登陆的话,用httpwatch能看到他发送了.account.xiaomi.com中的cookie,为什么使用程序get的时候他就不发送.account.xiaomi.com中的cookie了。甚为纠结。。。
希望各位大大指点迷津。。。让我不要再纠结了。。。

    private HttpWebResponse loginResponse;
    private StreamReader loginRespReader;

    private CookieContainer m_Cookie = new CookieContainer();
    private void button1_Click(object sender, EventArgs e)
    {
        this.xiaomiLogin();
        string str=this.loginRespReader.ReadToEnd();

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://account.xiaomi.com/pass/userInfo?userId=174314059");
        req.CookieContainer = new CookieContainer();
        req.CookieContainer = m_Cookie;
        req.Method = "GET";
        //req.ContentType = "application/x-www-form-urlencoded";
        loginResponse = (HttpWebResponse)req.GetResponse();
        loginRespReader = new StreamReader(loginResponse.GetResponseStream());
        textBox1.Text = loginRespReader.ReadToEnd();
    }

    private void xiaomiLogin()
    {          

        string requestUriString = "https://account.xiaomi.com/pass/serviceLoginAuth2";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUriString);
        request.CookieContainer = new CookieContainer();
        request.CookieContainer = m_Cookie;
        request.Method = "POST";
        request.AllowAutoRedirect = true;
        request.ContentType = "application/x-www-form-urlencoded";
        string s = this.quoteParas(paras);
        byte[] bytes = Encoding.UTF8.GetBytes("passToken=&user=13763114744&pwd=xiaomi123&callback=https%3A%2F%2Faccount.xiaomi.com&sid=passport&qs=%253Fsid%253Dpassport&hidden=&_sign=KKkRvCpZoDC%2BgLdeyOsdMhwV0Xg%3D");
        request.ContentLength = bytes.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);
        this.loginResponse = (HttpWebResponse)request.GetResponse();
        m_Cookie = request.CookieContainer;

        this.loginRespReader = new StreamReader(this.loginResponse.GetResponseStream());

    }

1个回答

u012481548
u012481548   2013.10.17 17:52
已采纳

tring requestUriString = "https://account.xiaomi.com/pass/serviceLoginAuth2";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUriString);
request.CookieContainer = new CookieContainer();
request.CookieContainer = m_Cookie;
request.Method = "POST";
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
string s = this.quoteParas(paras);
byte[] bytes = Encoding.UTF8.GetBytes("passToken=&user=13763114744&pwd=xiaomi123&callback=https%3A%2F%2Faccount.xiaomi.com&sid=passport&qs=%253Fsid%253Dpassport&hidden=&_sign=KKkRvCpZoDC%2BgLdeyOsdMhwV0Xg%3D");
request.ContentLength = bytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
this.loginResponse = (HttpWebResponse)request.GetResponse();
m_Cookie = request.CookieContainer;

    this.loginRespReader = new StreamReader(this.loginResponse.Get
Csdn user default icon
上传中...
上传图片
插入图片