I'm trying to write a small program that logs in to the Verizon website and then checks how many minutes are left for the month. I need help figuring out how to login to the site using C#. I know I need to use webrequest to post the login information, but I'm not sure how to go about doing it. The site with the login form is https://login.verizonwireless.com/amserver/UI/Login, but I'm not sure what information I have to post to the site to login and how to do it. Below is what I found for the source of the website. If someone can help开发者_StackOverflow me figure out how to login from a C# program I would greatly appreciate it. Thank You for any help.
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;">Sign In to My Verizon div class="clear10"> /div>
First of all, you're missing the two important fields :) If you look at the HTML, there are two additional fields in the form — IDToken1
(which is username), and IDToken2
(which is password).
If you supply these to the POST request you should get back some cookies which you can then use on subsequent requests. These will identify you as a logged-in user.
Of course, I can't fully test this since I don't have a valid login, but here's a beginning:
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());
}
}
Here are 2 functions you need to do this. Yes you are correct, you must use the webrequest, but there is a callback that you need to forge for the validation of the cert from the https. You should be able to use these right out og the box.
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
精彩评论