无法发布请求登录

时间:2018-08-29 00:56:38

标签: c# httpwebrequest webrequest httpwebresponse

我正在尝试登录网站。我正在使用httpwebrequest作为后登录参数,然后试图获取新页面的源代码。但这给了我“错误请求”; 我的代码的错误在哪里?

string formUrl = "https://tr.ogame.gameforge.com:443/main/login"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
        string formParams = string.Format("credentials[email]={0}&credentials[password]={1}&autologin={2}&language={3}&kid={4}", "myUsername", "myPassword","false","tr","");
        string cookieHeader;
        WebRequest req = WebRequest.Create(formUrl);
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(formParams);
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }
        WebResponse resp = req.GetResponse();
        cookieHeader = resp.Headers["Set-cookie"];
        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        {
            string pageSource = sr.ReadToEnd();
            txtb.Text = pageSource;
        }

这是我得到的结果;

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>400 Bad Request</title>
    </head><body>
    <h1>Bad Request</h1>
    <p>The request could not be understood by the server.</p>
    </body></html>

这是网页请求标头; Screenshot1 Screenshot2

0 个答案:

没有答案