繁体   English   中英

C#中的HttpWebRequest Auth发布请求

[英]HttpWebRequest Auth Post Request in c#

我正在使用打p西装来检查请求,我试图将其转换为C#代码

POST /sso HTTP/1.1
Host: account.ankama.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Referer: http://www.dofus.com/fr
Cookie: LANG=fr; _ga=GA1.1.1197518596.1489526959; SID=452EDCF3C4BD32057F9F08254BE40001
Connection: close
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
Content-Length: 102

action=login&from=http%3A%2F%2Fwww.dofus.com%2Ffr&login=user123&password=password1232F&remember=1

所以我试图:

        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://account.ankama.com/sso?action=login&from=https%3A%2F%2Faccount.ankama.com%2Ffr%2Fsecurite%2Fmode-restreint%3Ff%3Dhttps%3A%2F%2Faccount.ankama.com%2Ffr%2Fidentification%3Ff%3Dhttps%3A%2F%2Faccount.ankama.com%2Ffr%2Fcompte%2Finformations&login=user111&password=password1472F");
        Request.ContentType = "application/x-www-form-urlencoded";
        Request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        Request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0";
        Request.Host = "account.ankama.com";         
        Request.Referer = "https://account.ankama.com/fr/votre-compte/profil";
        Request.Method = "POST";
        Request.AllowAutoRedirect = true;
        Request.CookieContainer = new CookieContainer();
        //quest.Credentials = new NetworkCredential("user123", "passowrd123");
        using (HttpWebResponse response = (HttpWebResponse)Request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream);
                StreamWriter writer = new StreamWriter("odm.html");
                writer.Write(reader.ReadToEnd());
                writer.Close();
                reader.Close();
                Console.WriteLine("Done");
            }

        }

        Console.ReadKey();

在odm.html文件中,我正在检查html代码是否包含用户实际登录时显示的“我的帐户”。 但这由于我仍然不知道的某些原因似乎没用。 我对HTTP状态代码进行了一些研究,但是在尝试使用实际现有帐户和无效帐户登录后,我的brup服中出现了相同的http代码302,但内容长度不同。

编辑:问题是我在html文件中找不到“我的帐户”,我只找到用户要登录的页面

您正在尝试通过查询字符串发送请求正文,您正在将请求方法设置为POST但没有发送正文。 请求网址应为:

https://account.ankama.com/sso

并且您需要在发送请求之前设置请求正文:

var bytes = Encoding.UTF8.GetBytes("action=login&from=http%3A%2F%2Fwww.dofus.com%2Ffr&login=user123&password=password1232F&remember=1");
request.ContentLength = bytes.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(bytes, 0, bytes.Length);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM