使用C#登录https站点

时间:2011-01-29 22:45:24

标签: c#

我正在尝试编写一个登录Verizon网站的小程序,然后检查该月剩余的分钟数。我需要帮助找出如何使用C#登录网站。我知道我需要使用webrequest发布登录信息,但我不知道如何去做。登录表单的网站是https://login.verizonwireless.com/amserver/UI/Login,但我不确定我要将哪些信息发布到网站以进行登录以及如何进行登录。以下是我发现的网站来源。如果有人可以帮我弄清楚如何从C#程序登录,我将非常感激。谢谢你的帮助。

  

form method =“post”autocomplete =“off”   action =“https://login.verizonwireless.com:443/amserver/UI/Login”name =“loginForm”id =“loginForm”onsubmit =“return disableBut();”>
  input type =“hidden”name =“realm”value =“vzw”/>
  input type =“hidden”name =“goto”value =“”/>
  input type =“hidden”name =“gotoOnFail”value =“”/>
  input type =“hidden”name =“gx_charset”value =“UTF-8”/>
  input type =“hidden”name =“rememberUserNameCheckBoxExists”value =“Y”/>
  h2 style =“padding-left:0px;”>登录我的Verizon
  div class =“clear10”> / DIV>

2 个答案:

答案 0 :(得分:2)

首先,您错过了两个重要字段:)如果您查看HTML,表单中还有两个字段 - IDToken1(用户名)和IDToken2 (这是密码)。

如果您将这些提供给POST请求,则 应该返回一些cookie,然后您可以在后续请求中使用这些cookie。这些将识别您为登录用户。

当然,我无法完全测试这个,因为我没有有效的登录,但这是一个开始:

class VerizonLogin
{
    CookieContainer Cookies = new CookieContainer();

    void Main()
    {
        Login("test","testpass");

        // Now the cookies in "Cookies" are all set.
        // Ensure you set CookieContainer on all subsequent requests
    }

    void Login(string username, string password)
    {
        var wr = (HttpWebRequest)WebRequest.Create("https://login.verizonwireless.com:443/amserver/UI/Login");
        wr.Method = "POST";
        wr.ContentType = "application/x-www-form-urlencoded";
        wr.Referer = "https://login.verizonwireless.com/amserver/UI/Login"; // my tests show this is needed
        wr.CookieContainer = Cookies;

        var parameters = new Dictionary<string,string>{
            {"realm", "vzw"},
            {"goto",""},
            {"gotoOnFail",""},
            {"gx_charset", "UTF-8"},
            {"rememberUserNameCheckBoxExists","Y"},
            {"IDToken1", username},
            {"IDToken2", password}
        };

        using (var requestStream = wr.GetRequestStream())
        using (var writer = new StreamWriter(requestStream,Encoding.UTF8))
            writer.Write(ParamsToFormEncoded(parameters));

        using (var response = (HttpWebResponse)wr.GetResponse())
        {
            // here you need to detect a correct login... this might be one of the cookies.
            // if incorrect throw an exception or something.
        }
    }

    string ParamsToFormEncoded(Dictionary<string,string> parameters)
    {
        return string.Join("&", parameters.Select(kvp => 
            Uri.EscapeDataString(kvp.Key).Replace("%20","+") + "=" + Uri.EscapeDataString(kvp.Value).Replace("%20","+")
        ).ToArray());
    }
}

答案 1 :(得分:1)

以下是您需要执行此操作的2个功能。是的,你是对的,你必须使用webrequest,但是你需要伪装一个回调来验证来自https的证书。您应该能够在框中使用这些。

C#

private bool ValidateCert(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    return true;
}

private string PostToSite(string url)
{
    string result = string.empty;

    byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes("realm=vzw&goto=&gotoOnFail=&gx_charset=UTF-8&rememberUserNameCheckBoxExists=Y");

    HttpWebRequest webRequest = (HttpWebRequest)Net.WebRequest.Create(_endpoint);
    webRequest.KeepAlive = false;
    webRequest.AllowAutoRedirect = false;

    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateCert);

    webRequest.ContentLength = postBuffer.Length;
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";

    using (Stream str = webRequest.GetRequestStream()) {
        str.Write(postBuffer, 0, postBuffer.Length);
    }

    using (System.Net.HttpWebResponse res = (HttpWebResponse)webRequest.GetResponse()) {
        using (StreamReader sr = new StreamReader(res.GetResponseStream())) {
            result = sr.ReadToEnd();
        }
    }

    return result;
}

VB.NET

Private Function ValidateCert(ByVal sender As Object, ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
 Return True
End Function

Private Function PostToSite(url as string) as string
    Dim result as string = string.empty

    Dim postBuffer As Byte() = System.Text.Encoding.GetEncoding(1252).GetBytes("realm=vzw&goto=&gotoOnFail=&gx_charset=UTF-8&rememberUserNameCheckBoxExists=Y")

    Dim webRequest As HttpWebRequest = CType(Net.WebRequest.Create(_endpoint), HttpWebRequest)
    webRequest.KeepAlive = False
    webRequest.AllowAutoRedirect = False

    ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf ValidateCert)

    webRequest.ContentLength = postBuffer.Length
    webRequest.Method = "POST"
    webRequest.ContentType = "application/x-www-form-urlencoded"

    Using str As Stream = webRequest.GetRequestStream()
        str.Write(postBuffer, 0, postBuffer.Length)
    End Using

    Using res As System.Net.HttpWebResponse = CType(webRequest.GetResponse(), HttpWebResponse)
        Using sr As New StreamReader(res.GetResponseStream())
            result = sr.ReadToEnd()
        End Using
    End Using

    return result
End Function