且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

登录网站,通过C#

更新时间:2023-12-05 14:00:16

您可以继续使用Web客户端为POST(而不是GET,这是的 HTTP动词您正在使用与DownloadString),但我认为你会发现很容易与(略)低层阶级的WebRequest工作和WebResponse。

You can continue using WebClient to POST (instead of GET, which is the HTTP verb you're currently using with DownloadString), but I think you'll find it easier to work with the (slightly) lower-level classes WebRequest and WebResponse.

有两个部分本 - 首先是张贴登录表单,二是恢复设置Cookie头和发送您的GET请求沿着回服务器为曲奇。服务器将使用这个cookie从现在开始(假设它使用基于cookie的验证,而我相当有信心它是作为该页面返回一个Set-Cookie头,其中包括PHPSESSID)。确定你

There are two parts to this - the first is to post the login form, the second is recovering the "Set-cookie" header and sending that back to the server as "Cookie" along with your GET request. The server will use this cookie to identify you from now on (assuming it's using cookie-based authentication which I'm fairly confident it is as that page returns a Set-cookie header which includes "PHPSESSID").


张贴到登录表单

表的帖子很容易模仿,它只是格式化后的数据如下的情况:

Form posts are easy to simulate, it's just a case of formatting your post data as follows:

field1=value1&field2=value2

使用的WebRequest和code我改编自Scott Hanselman的,这里是你如何表单POST数据到您的登录表单:

Using WebRequest and code I adapted from Scott Hanselman, here's how you'd POST form data to your login form:

string formUrl = "http://www.mmoinn.com/index.do?PageModule=UsersAction&Action=UsersLogin"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = string.Format("email_address={0}&password={1}", "your email", "your 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"];

下面是你应该在Set-cookie头看看你的登录表单的例子:

Here's an example of what you should see in the Set-cookie header for your login form:

PHPSESSID=c4812cffcf2c45e0357a5a93c137642e; path=/; domain=.mmoinn.com,wowmine_referer=directenter; path=/; domain=.mmoinn.com,lang=en; path=/;domain=.mmoinn.com,adt_usertype=other,adt_host=-



获取页面的登录表单后面

现在您就可以执行GET请求,你需要先登录一个页面。

Now you can perform your GET request to a page that you need to be logged in for.

string pageSource;
string getUrl = "the url of the page behind the login";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
    pageSource = sr.ReadToEnd();
}

编辑:

如果您需要查看第一篇文章的结果,可以恢复其与返回的HTML:

If you need to view the results of the first POST, you can recover the HTML it returned with:

using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
    pageSource = sr.ReadToEnd();
}

这个广场正下方 cookieHeader = resp.Headers [设置Cookie]; ,然后视察pageSource举行字符串

Place this directly below cookieHeader = resp.Headers["Set-cookie"]; and then inspect the string held in pageSource.