大家好,我正在尝试以编程方式登录网站。我已经对此进行了处理,但它是一个 php 页面,我使用下面的代码(在 Stack Overflow 上的某个位置)登录它并且效果很好。
private static string GetDataFromPHP(string formUrl, string getUrl, string username, string password, out bool status)
{
try
{
string formParams = string.Format("access_login={0}&access_password={1}", username, password);
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"];
string pageSource;
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
status = true;
return pageSource;
}
catch (System.Exception ex)
{
status = false;
return string.Empty;
}
}
其中 access_login 和 access_password 是接受凭据的输入框的名称。我不知道如何为 iis 登录提示实现它,如下所示。请帮助