2017-03-15 110 views
0
使用打嗝西裝檢查請求和IM試圖convertthis到C#代碼

HttpWebRequest的驗證POST請求在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 

所以我試圖

我':在文件ODM

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

.html我檢查是否該代碼包含用戶實際登錄時顯示的「我的帳戶」。 但這似乎沒有工作的原因,我仍然不知道。 我做了一些關於HTTP狀態代碼的研究,但是在嘗試使用一個實際的現有帳戶和一個沒有有效的帳戶登錄後,它給出了具有不同內容長度的相同的http代碼302。

編輯: 問題是我不會在HTML文件中找到「我的帳戶」,我只找到其中​​用戶要登錄

+0

看樣子你有敏感信息,出示您的C#查詢字符串。我建議刪除它們。 – StfBln

+0

您是否嘗試將Cookie添加到您的請求中?通常我會採取一切辦法,然後逐個去除看似無用的碎片。 – StfBln

+0

我試穿了我的打嗝套裝,發送了沒有它們的必需品,它的作品,所以他們真的很重要 – Huster

回答

1

您要發送請求正文頁面查詢字符串,您將請求方法設置爲POST,但您沒有發送正文。請求URL應該是:

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); 
} 
+0

touhght! ,我現在試試吧 – Huster

+0

它的作用先生!除了m得到一個異常「我不能設置request.ContentLength屬性,同時閱讀流 – Huster

+1

@Huster okey,我更新了代碼,只是將ContentLength設置爲使用語句,應該沒問題。 –