登录网站,通过C# [英] Login to website, via C#

查看:138
本文介绍了登录网站,通过C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的使用C#,并有一个网站上读取源$ C ​​$ C部分的应用程序。这所有的作品;但问题是,有问题的页面需要登录后才能访问这个源$ C ​​$ C用户。什么我的程序需要一种方式来初次登录用户进入网站 - 做到这一点后,我就可以访问和读取源$ C ​​$ C。

I'm relatively new to using C#, and have an application that reads parts of the source code on a website. That all works; but the problem is that the page in question requires the user to be logged in to access this source code. What my program needs a way to initially log the user into the website- after that is done, I'll be able to access and read the source code.

这需要登录到该网站是:
mmoinn.com/index.do?PageModule=UsersLogin

The website that needs to be logged into is: mmoinn.com/index.do?PageModule=UsersLogin

我搜索有关如何做到这一点,并试图例子了整整一天,但有没有运气。

I've searched for the entire day about how to do this and tried examples, but have had no luck.

在此先感谢

推荐答案

您可以继续使用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我改编自<一个href=\"http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx\">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.

这篇关于登录网站,通过C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆