HttpWebRequest login

本文关键字:login HttpWebRequest | 更新日期: 2023-09-27 17:56:07

我正在尝试登录一个https网站,当我运行代码时,我没有收到错误,但我也没有登录,所以我想知道问题是什么我对 http 网站使用相同的代码和技术(显然具有不同的 postdata 字符串),它让我登录这是代码:

string postData = "JAVASCRIPT_ON=userName=XXXX&password=XXX&loginAction=Logon";
            CookieContainer tempCookies = new CookieContainer();
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteData = encoding.GetBytes(postData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://direct.gov.mb.ca/ppr/actions/loginDispatcher");
            request.Method = "POST";
            request.KeepAlive = true;
            request.CookieContainer = tempCookies;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Referer = "https://direct.gov.mb.ca/ppr/jsps/login/login.jsp";
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
            request.ContentLength = postData.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(byteData, 0, byteData.Length);
            requestStream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        tempCookies.Add(response.Cookies);
        loginCookies = tempCookies;

HttpWebRequest login

您应该将所有

请求的 CookieContainer 属性设置为同一实例。