为特殊的HTML输入创建一个HttpWebRequest
本文关键字:一个 HttpWebRequest 创建 HTML 输入 | 更新日期: 2023-09-27 18:02:03
我需要发送一个HttpWebRequest到一个url与电子邮件和密码
<input name="Login[email]" id="Login_email" type="text" />
<input name="Login[password]" id="Login_password" type="password" />
<input type="hidden" value="f3b0997e3b6451ecee2840f353b3cf9a69805596" name="fs_csrf" />
如何创建HttpWebRequest在服务器端与电子邮件,密码和隐藏的输入字段上面。提前感谢您
试试这个:
//getting the hash
var req = (HttpWebRequest)WebRequest.Create("https://example.com/login");
var cookies = new CookieContainer();
req.CookieContainer = cookies;
string fs_csrf;
using (var res = req.GetResponse())
using (var resStream = res.GetResponseStream())
using (var reader = new StreamReader(resStream)) {
fs_csrf = new Regex("input.*?hidden.*?value='"(.*?)'"").Match(reader.ReadToEnd()).Groups[1].Value;
}
//submitting form
req = (HttpWebRequest)WebRequest.Create("https://example.com/login");
req.CookieContainer = cookies;
req.Method = "POST"; //i assume it's a post request
var buffer = Encoding.UTF8.GetBytes("Login[email]=theemail&Login[password]=thepswd&fs_csrf=" + fs_csrf);
req.ContentLength = buffer.Length;
using (var reqStream = req.GetRequestStream()) {
reqStream.Write(buffer, 0, buffer.Length);
}
using (var res = req.GetResponse())
using (var resStream = res.GetResponseStream())
using (var reader = new StreamReader(resStream)) {
var respStr = reader.ReadToEnd();
}