简体   繁体   English

C#中的HttpWebRequest Auth发布请求

[英]HttpWebRequest Auth Post Request in c#

i'am using burp suit to check the requests and im trying to convertthis to c# code 我正在使用打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

So i tried to : 所以我试图:

        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();

in the file odm.html I m checking if the html code contain "My account" that shown when the user is actually logged in . 在odm.html文件中,我正在检查html代码是否包含用户实际登录时显示的“我的帐户”。 but this doesnt seems to be working for some reasons that i still don't know . 但这由于我仍然不知道的某些原因似乎没用。 i made some research to about HTTP status code but in my brup suit after trying to login in with an actual exisiting account and a none valid account it gives the same http code 302 with a different Content Length . 我对HTTP状态代码进行了一些研究,但是在尝试使用实际现有帐户和无效帐户登录后,我的brup服中出现了相同的http代码302,但内容长度不同。

EDIT: the issue is i don't find 'my account' in the html file , i only find the page where the user is going to login 编辑:问题是我在html文件中找不到“我的帐户”,我只找到用户要登录的页面

You are trying to send request body in query string, you are setting the request method as POST but you are not sending the body. 您正在尝试通过查询字符串发送请求正文,您正在将请求方法设置为POST但没有发送正文。 The request url should be: 请求网址应为:

https://account.ankama.com/sso

And you need to set request body before sending the request: 并且您需要在发送请求之前设置请求正文:

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