如何使用HttpWebRequest的登录到网站? [英] How to use HttpWebRequest to login to website?

查看:145
本文介绍了如何使用HttpWebRequest的登录到网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改写某些服务器的PHP代码,登录到一个网站,使用规范的HttpWebRequest使用遍布于C#网站网:

I am trying to rewrite some server php code which logs into a website, using the canonical HttpWebRequest usage found all over the net on C# sites:

HttpWebRequest BuildPOST(string url, string parameters)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    byte[] bytes = Encoding.ASCII.GetBytes(parameters);
    Stream os = null;
    try
    { 
         request.ContentType = "application/x-www-form-urlencoded";
         equest.ContentLength = bytes.Length;
         os = request.GetRequestStream();
         os.Write(bytes, 0, bytes.Length); 
    }
    catch (WebException ex)
    {
         Console.WriteLine("{0} HttpPost: Request error", ex.Message);
    }
    finally
    {
         if (os != null)
         {
              os.Close();
         }
    }
    return request;
}



使用名为:

called using:

string login_url = "http://www.sailonline.org/community/accounts/login/";
string login_post_data = "next=/windy/run/{0}/&password={1}&username={2}";  // race, pwd, boat
HttpWebRequest req = BuildPOST(login_url, string.Format(login_post_data, race, pwd, user));



原来PHP是:

The original php is:

include 'phplib.php';

$url = "http://www.sailonline.org/community/accounts/login/";
$postdata = sprintf("next=/windy/run/%s/&password=%s&username=%s", $race, $key, $boat);
$html = get_pipe_output(build_post_url($url, $postdata));



不过,C#代码不会产生从服务器的PHP代码相同的反应。相反,我得到一个网页询问登录信息(这是正确地在后一起开始输入)。

However, the C# code does not generate the same response from the server as the php code. Instead I get a page asking for login details (which were correctly input in the post to begin with).

我有些新的网络编程,只是似乎无法弄清楚这是为什么happeneing。我从我的代码窃听报文后,为这一网站上的PHP和原来的登录页面,无法看到的页面中的任何悫的requsts区别,只是一个从C#代码发行不有预期的反应。结果
所有我能想到的也许是PHP函数做一些我不知道的?

I am somewhat new to network programming, and just cannot seem to figure out why this is happeneing. I have snooped the packets from my code, the page serving the php and the original login page on the website in question and cannot see any difference betwen the requsts, only that the one issued from C# code does not have the expected response.
All I can think is perhaps the php functions do something I am unaware of ??

推荐答案

您需要创建和设置的请求的CookieContainer。如果没有,您的请求不会成功。 。貌似页面发送的响应cookie

You need to create and set a CookieContainer on the request. Without that your request wont succeed. Looks like the page is sending cookies with the response.

HTTP/1.1 200 OK
Date: Mon, 31 Jan 2011 23:53:23 GMT
Server: Apache
Expires: Mon, 31 Jan 2011 23:53:23 GMT
Vary: Cookie
Last-Modified: Mon, 31 Jan 2011 23:53:23 GMT
ETag: "ad806aa693ed8187c278f0fadfa92d01"
Cache-Control: max-age=0
Content-Type: text/html; charset=utf-8
Set-Cookie:  sailonlinesid=bcc2b5fe9980df3e741e8fe7279d61d4; Domain=.sailonline.org; expires=Mon, 14-Feb-2011 23:53:23 GMT; Max-Age=1209600; Path=/
Connection: close

这篇关于如何使用HttpWebRequest的登录到网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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