HttpWebRequest在c#中验证发布请求

时间:2017-03-15 15:08:14

标签: c#

我正在使用打嗝套装检查请求,我试图将其转换为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文件中,我只找到用户要登录的页面

1 个答案:

答案 0 :(得分:1)

您正在尝试在查询字符串中发送请求正文,您正在将请求方法设置为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);
}