2011-11-09 95 views
0

我不知道如何以编程方式登录到此site 我已通过stackoverflow搜索并找到this,但我仍然不知道要将什么放入URL或URI。如何以编程方式登录到网站

+1

使用[本答案](http://stackoverflow.com/questions/930807/c-sharp-login-to-website-via-program/931030#931030)中的代码并更改URL和表单参数。 –

回答

4

当我只需输入用户名“ABC”和密码“高清”和按下按钮,我得到了下面的帖子数据:

下一个=应用程式%2Flinks%2F &为什么= PW &电子邮件= ABC &密码=高清& fw_human =

所以这使我beleive如果你只是使用POST数据和相应的信息替换它,你可以模拟手动登录。

所以从你链接的堆栈溢出,这将采取以下形式:

string formUrl = "http://ratings-plus.webs.com/apps/auth/doLogin"; // 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("next=apps%2Flinks%2F&why=pw&email={0}&password={1}&fw_human=", "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"]; 

注意,在将来的请求,你需要再使用任何返回给维护您的认证状态cookie的值。

+0

我试过了,但没有帮助。我仍然无法输入:http://ratings-plus.webs.com/apps/links/使用我的登录cookie :( – Qbunia

+0

我真的无法帮助了,因为我看不到头或任何成功的登录,要重用的cookie头可能包含在多个set-cookie头中,很难说没有看到响应数据。您是否使用过像Fiddler或Firebug这样的工具或Chome开发工具来观察网络流量?这样您就可以查看手动登录的请求和响应标题和数据,并将其与您的程序化登录进行比较以查看缺失的部分。 –