HttpWebRequest 的基本介绍
首先,您需要合适的工具来完成这项工作。去下载实时 HTTP 标头火狐浏览器插件。这将允许您实时查看 HTTP 标头,以便您可以查看与网站交互时发送的 POST 数据。了解发送到网站的数据后,您可以通过以编程方式创建自己的 HTTP Web 请求来模拟该过程。工具 > 实时 HTTP 标头
通过导航到加载实时 HTTP 标头工具 > 实时 HTTP 标头。一旦你加载了 GUI,导航到你想要登录的网站,我将使用Facebook出于演示目的。输入您的凭据以准备登录,但在此之前请清除 GUI 文本窗口并确保标记为复选框Capture已检查。点击登录后,您将看到文本窗口充斥着有关请求的各种信息,包括您需要的 POST 数据。
我发现最好点击保存全部...然后在文本文档中搜索您的用户名,以便您可以轻松识别 POST 数据。对于我的请求,POST 数据如下所示:
lsd=AVp-UAbD&display=&legacy_return=1&return_session=0&trynum=1&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C %D0%94%2C%D0%84&timezone=0&lgnrnd=214119_mDgc&lgnjs=1356154880&email=%myfacebookemail40outlook.com&pass=myfacebookpassword&default_persistent=0
然后可以在 C# 中定义如下:
StringBuilder postData = new StringBuilder();
postData.Append("lsd=AVqRGVie&display=");
postData.Append("&legacy_return=1");
postData.Append("&return_session=0");
postData.Append("&trynum=1");
postData.Append("&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84");
postData.Append("&timezone=0");
postData.Append("&lgnrnd=153743_eO6D");
postData.Append("&lgnjs=1355614667");
postData.Append(String.Format("&email={0}", "CUSTOM_EMAIL"));
postData.Append(String.Format("&pass={0}", "CUSTOM_PASSWORD"));
postData.Append("&default_persistent=0");
我的目的是向您展示我们可以通过 Web 浏览器“手动”发送的 POST 数据之间的关系,以及我们如何使用所述数据来模拟 C# 中的请求。了解发送 POST 数据远非确定性的。不同的网站以不同的方式工作,并且可以为您提供各种信息。下面是我整理的一个函数,用于验证 Facebook 凭据是否正确。我不能也不应该在这里深入探讨,因为课程及其成员都有很好的自我记录。关于所使用的方法,您可以找到比我能提供的更好的信息MSDN例如,WebRequest.Method 属性
private bool ValidateFacebookCredentials(string email, string password)
{
CookieContainer cookies = new CookieContainer();
HttpWebRequest request = null;
HttpWebResponse response = null;
string returnData = string.Empty;
//Need to retrieve cookies first
request = (HttpWebRequest)WebRequest.Create(new Uri("https://www.facebook.com/login.php?login_attempt=1"));
request.Method = "GET";
request.CookieContainer = cookies;
response = (HttpWebResponse)request.GetResponse();
//Set up the request
request = (HttpWebRequest)WebRequest.Create(new Uri("https://www.facebook.com/login.php?login_attempt=1"));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
request.Referer = "https://www.facebook.com/login.php?login_attempt=1";
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.CookieContainer = cookies;
//Format the POST data
StringBuilder postData = new StringBuilder();
postData.Append("lsd=AVqRGVie&display=");
postData.Append("&legacy_return=1");
postData.Append("&return_session=0");
postData.Append("&trynum=1");
postData.Append("&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84");
postData.Append("&timezone=0");
postData.Append("&lgnrnd=153743_eO6D");
postData.Append("&lgnjs=1355614667");
postData.Append(String.Format("&email={0}", email));
postData.Append(String.Format("&pass={0}", password));
postData.Append("&default_persistent=0");
//write the POST data to the stream
using(StreamWriter writer = new StreamWriter(request.GetRequestStream()))
writer.Write(postData.ToString());
response = (HttpWebResponse)request.GetResponse();
//Read the web page (HTML) that we retrieve after sending the request
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
returnData = reader.ReadToEnd();
return !returnData.Contains("Please re-enter your password");
}