.NET HTTPS请求 - 发布数据以登录站点

时间:2012-07-06 14:16:41

标签: c# .net http https web-scraping

我正在尝试建立一项服务,以登录网上银行网站并下载用户的最新交易信息。

我正在使用.NET / C#构建它,我正在尝试按照我在另一篇文章中看到的关于如何执行此操作的示例... .NET HTTP POST Method - Cookies issue

但我似乎无法让这个例子起作用。

我要登录的网站是...... http://www.communityamericacu.com/

这是我到目前为止所提出的......

public void RetrieveTransactions()
{
    var request = (HttpWebRequest) WebRequest.Create("https://secureaccess.cacu.com/accountlink/SignOn/SignOn.aspx");
    var cookies = new CookieContainer();
    request.CookieContainer = cookies;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";

    using (var requestStream = request.GetRequestStream())
    {
        using (var writer = new StreamWriter(requestStream))
        {
            writer.Write("user=****&PIN=****&BankFolder=Banking&BrandID=1&BrandFolder=cacu&BrandType=R&screenwidth=&screenheight=&RemoteLogin=N&ExitURL=&UserJavaPlatForm=Win32&UserJavaBrowser=Netscape&UserJavaVersion=5.0+%28Windows+NT+6.1%3B+WOW64%29+AppleWebKit%2F536.11+%28KHTML%2C+like+Gecko%29+Chrome%2F20.0.1132.47+Safari%2F536.11&UserJavaCodeName=Mozilla&UserJavaColorDepth=32&UserJavaColors=4294967296&UserJavaCurrentResolution=1600x900&UserJavaMaxResolution=1600x870&UserJavaEnabled=Yes&UserJavaAAFonts=No&UserJavaPlugins=+Remoting+Viewer+Native+Client+Chrome+PDF+Viewer+Shockwave+Flash+Shockwave+Flash+Adobe+Acrobat+Microsoft+Lync+2010+Meeting+Join+Plug-in+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+Microsoft+Office+2010+Microsoft+Office+2010+Google+Update+NVIDIA+3D+Vision+NVIDIA+3D+VISION+Java%28TM%29+Platform+SE+7+U5+Java+Deployment+Toolkit+7.0.50.255+Shockwave+for+Director+Silverlight+Plug-In+Remoting+Viewer+Native+Client+Chrome+PDF+Viewer+Shockwave+Flash+Shockwave+Flash+Adobe+Acrobat+Microsoft+Lync+2010+Meeting+Join+Plug-in+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+QuickTime+Plug-in+7.7.2+Microsoft+Office+2010+Microsoft+Office+2010+Google+Update+NVIDIA+3D+Vision+NVIDIA+3D+VISION+Java%28TM%29+Platform+SE+7+U5+Java+Deployment+Toolkit+7.0.50.255+Shockwave+for+Director+Silverlight+Plug-In&UserJavaUTCOffset=5");
        }
    }

    using (var responseStream = request.GetResponse().GetResponseStream())
    {
        if (responseStream != null)
        {
            using (var reader = new StreamReader(responseStream))
            {
                var result = reader.ReadToEnd();
            }
        }
    }

}

但这是我得到的回应......

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>@ccountLink Error</title>
</head>
<body>
An error occurred.
</body>
</html>

当我尝试登录网站时,即使用不正确的用户名和密码,这也不是我得到的。

在这一点上,这个东西有点过头了。我能得到的任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

也许银行会检查HTTP请求的其他字段,例如User-Agent(即浏览器),当它找不到它时会出错?尝试至少添加用户代理(在您链接到的问题上有一个示例)。也许你还需要添加其他字段。您可以在此处查看完整列表http://en.wikipedia.org/wiki/List_of_HTTP_header_fields