我试图利用已经在我们网站的某个部分完成的功能登录到该网站,然后像我在浏览器中那样与其进行交互。可能有一个更好的方法,所以这将是受欢迎的。不过,我限制使用C#。
这个表格在一个空白的网页上成功登录到网站(我当然是虚构的):
<input id="EmailAddress" name="EmailAddress" type="text" value="theprocess@oursite.com" />
<input id="Password" name="Password" type="password" value="avalidpassword" />
<button type="submit" >Log in</button>
我试过这个没有成功。它只是不断重新加载相同的登录页面。
var request = (HttpWebRequest)WebRequest.Create("https://oursite.com/Admin/Account/Login");
string postData = "EmailAddress=theprocess@oursite.com";
postData += "&Password=avalidpassword";
byte[] data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
我也试过这个:
string URI = "https://myaccount.boom.us/Admin/Account/Login";
string postData = "EmailAddress=theprocess@oursite.com";
postData += "&Password=avalidpassword";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, postData);
}
它也只是重新加载相同的登录页面。
我做错了什么?