程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> httpwebrequest-C# 模擬post登陸後,關鍵cookie發送不出去,求幫助

httpwebrequest-C# 模擬post登陸後,關鍵cookie發送不出去,求幫助

編輯:編程綜合問答
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());

    }

最佳回答:


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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved