Authentication 以编程方式登录到网页HTTPS(C#)

Authentication 以编程方式登录到网页HTTPS(C#),authentication,https,httpwebrequest,Authentication,Https,Httpwebrequest,我正试图以编程方式登录到一个网页,我感到非常高兴。我看了看fiddler,看看浏览器在做什么,但我正在努力用代码复制它。 这是到目前为止的代码 private void VisitUrl() { string pageVisited = string.Empty; CookieContainer cookies = new CookieContainer(); //Go to login page so that we get a cookie HttpWebR

我正试图以编程方式登录到一个网页,我感到非常高兴。我看了看fiddler,看看浏览器在做什么,但我正在努力用代码复制它。 这是到目前为止的代码

private void VisitUrl()
{
    string pageVisited = string.Empty;
    CookieContainer cookies = new CookieContainer();

    //Go to login page so that we get a cookie
    HttpWebRequest loginRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.apage.com/login.asp");

    loginRequest.UseDefaultCredentials = true;
    loginRequest.UserAgent = Constants.usrAgentIE8;
    loginRequest.CookieContainer = cookies;
    loginRequest.AllowAutoRedirect = false;

    HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();

    string guid = Regex.Match(loginResponse.Headers["Set-Cookie"], Constants.guidRegex).Value;
    guid = guid.Replace("%2D", "-");

    //Now that we have the cookie we can do a proper login
    HttpWebRequest doLogin = (HttpWebRequest)HttpWebRequest.Create("https://secure.apage.com/express/login.asp");

    //doLogin.UseDefaultCredentials = true;
    doLogin.UserAgent = Constants.usrAgentIE8;
    doLogin.Method = Constants.verbPOST;
    doLogin.ContentType = Constants.contTypeURLenc;
    doLogin.AllowAutoRedirect = false;

    using (StreamWriter sw = new StreamWriter(doLogin.GetRequestStream()))
    {
        sw.Write("usr=user&pass=" + Constants.pass + "&mc=1&guid=" + guid + "&dbnum=0&ic=1&task=dologin&app=library&x=32&y=10");

    }

    doLogin.CookieContainer = cookies;
    doLogin.Referer = "http://www.apage.com/login.asp";

    HttpWebResponse Response = (HttpWebResponse)doLogin.GetResponse();

    string resphead = Response.Headers["Set-Cookie"];

    string resp = Response.StatusCode.ToString();

    using (StreamReader sr = new StreamReader(Response.GetResponseStream()))
    {
        string textresponse = sr.ReadToEnd();
    }

    HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create("http://www.apage.com/toc.aspx?id=40729");
    Request.UserAgent = Constants.usrAgentIE8;
    Request.UseDefaultCredentials = true;
    Request.CookieContainer = cookies;

    using (HttpWebResponse urlResponse = (HttpWebResponse)Request.GetResponse())
    {
        using (StreamReader reader = new StreamReader(urlResponse.GetResponseStream()))
        {
            pageVisited = reader.ReadToEnd();
        }
    }
}
我遇到的问题是,当我尝试第三个请求时,它只是缺少了一个cookie,似乎让我回到了登录页面。这是使用fiddler的浏览器成功登录:

 1  200 HTTP    CONNECT secure.apage.com:443    0           iexplore:5908           
 2  200 HTTPS   secure.apage.com    /express/login.asp  3,253   private     text/html   iexplore:5908           
 3  200 HTTPS   secure.apage.com    /express/zarketing.css  13,340      text/css    iexplore:5908           
 4  200 HTTPS   secure.apage.com    /express/images/_loadingtext.gif    798     image/gif   iexplore:5908           
 5  200 HTTP    CONNECT secure.apage.com:443    0           iexplore:5908           
 6  200 HTTP    CONNECT secure.apage.com:443    0           iexplore:5908           
 7  200 HTTP    CONNECT secure.apage.com:443    0           iexplore:5908           
 8  200 HTTP    CONNECT secure.apage.com:443    0           iexplore:5908           
 9  200 HTTP    CONNECT secure.apage.com:443    0           iexplore:5908           
 10 200 HTTPS   secure.apage.com    /express/images/_loadingbox1.gif    66      image/gif   iexplore:5908           
 11 200 HTTPS   secure.apage.com    /express/images/_loadingbox5.gif    66      image/gif   iexplore:5908           
 12 200 HTTPS   secure.apage.com    /express/images/_loadingbox4.gif    100     image/gif   iexplore:5908           
 13 200 HTTPS   secure.apage.com    /express/images/_loadingbox2.gif    66      image/gif   iexplore:5908           
 14 200 HTTPS   secure.apage.com    /express/images/_loadingbox3.gif    66      image/gif   iexplore:5908           
 15 302 HTTP    www.apage.com   /authenticate.asp?ticket=2ECCC1686F49BE35E14556E76EFCBF3E478157F89B826A03C421883BE61BB634D8DDDB25F8943414252CE46C27B1FAE837B802A7BD1EE9723ECDCB99B7D3D1DB60FAB3042D0D632BF1C41BA622C2546E&mc=1  142 max-age=0, no-cache, no-store  Expires: Fri, 10 Jun 2011 09:04:21 GMT   text/html   iexplore:5908           
 16 200 HTTP    www.apage.com   /toc.aspx?id=40729  86,179  max-age=0, no-cache, no-store  Expires: Fri, 10 Jun 2011 09:04:22 GMT   text/html; charset=utf-8    iexplore:5908           
请求15是一个重定向,这个请求已经有丢失的cookie,但是我不知道这个cookie是如何创建的。在我的第二个请求的响应中,它似乎不存在,我在调试时查看过

我需要请求所有的GIF吗?

您是否尝试过使用

doLogin.AllowAutoRedirect = true;
HTTP状态代码302(第15行)显示文件已“临时移动”。您可能需要允许重定向

此外,cookie信息也可以在
标题
集合属性中传递。在响应的
Headers.Keys
集合中查找任何名称,如“cookie”。例如,我必须登录的网站使用名称“Set Cookie”。我得到的值如下:

if (response.Headers["Set-Cookie"] != null)
{
    Match match = Regex.Match(response.Headers["Set-Cookie"].ToString(), @"(?'Name'\w+)
=(?'Value'\w+).+=(?'Path'[\s\S]+)");
    if (match.Success)
    {
        Cookie cookie = new Cookie(match.Groups["Name"].ToString(), 
match.Groups["Value"].ToString(), match.Groups["Path"].ToString(), "domain.name.com");
        cookies.Add(cookie);
    }
}
你试过使用吗

doLogin.AllowAutoRedirect = true;
HTTP状态代码302(第15行)显示文件已“临时移动”。您可能需要允许重定向

此外,cookie信息也可以在
标题
集合属性中传递。在响应的
Headers.Keys
集合中查找任何名称,如“cookie”。例如,我必须登录的网站使用名称“Set Cookie”。我得到的值如下:

if (response.Headers["Set-Cookie"] != null)
{
    Match match = Regex.Match(response.Headers["Set-Cookie"].ToString(), @"(?'Name'\w+)
=(?'Value'\w+).+=(?'Path'[\s\S]+)");
    if (match.Success)
    {
        Cookie cookie = new Cookie(match.Groups["Name"].ToString(), 
match.Groups["Value"].ToString(), match.Groups["Path"].ToString(), "domain.name.com");
        cookies.Add(cookie);
    }
}

不过,我相当肯定,我尝试过将allowautoredirect设置为true,但没有任何效果。等我有时间的时候,我可能会再试试。302状态码可能与此无关,但您是否检查了HTTP headers集合中的cookie信息?不过,我相当肯定我尝试过将allowautoredirect设置为true,但没有任何效果。等我有时间的时候,我可能会再来一次。302状态码可能与此无关,但您是否检查了HTTP headers集合中的cookie信息?标题有点误导。如果端口80未打开,则程序无效。互动程序应该是“通过HTTP(c#)以编程方式登录到HTTPS网页”,标题有点误导。如果端口80未打开,则程序无效。互动程序应该是“通过HTTP(c#)以编程方式登录到HTTPS网页”