0
所以我想要做的就是登錄到網站發送POST數據,然後做一個POST請求。我試着做一個web瀏覽器,只需登錄那裏,然後用一個按鈕提交POST數據。我不確定它是不是以這種方式工作,因爲它不是通過webbrowser1發送的,也不是因爲我沒有正確提交POST數據。這是提交POST數據的代碼。我如何可以獲取一個CSRF令牌或通過web瀏覽器
HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("url");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "postdata";
req.ContentLength = postData.Length;
StreamWriter stOut = new
StreamWriter(req.GetRequestStream(),
System.Text.Encoding.ASCII);
stOut.Write(postData);
stOut.Close();
我也嘗試了登錄只需按動一個按鈕,然後用另一個按鈕,提交此POST數據,但問題是,它需要POST請求裏面的CSRF令牌。這是我到目前爲止。
string formUrl = "loginurl";
string formParams = string.Format("username={0}&password={1}&csrfmiddlewaretoken={2}", "user", "pass", "token");
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"];
這樣做的最好方法是什麼?謝謝!