我现在想做GMAIL邮箱登陆器,但是GMAIL在登陆的时候是采用的HTTPS请求,我试了好多次都不行,我的代码是这样的:
首先的方法是获取登陆界面的表单元素的名和值,以生成请求数据:
private string GetQueryString()
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://mail.google.com");
myHttpWebRequest.Method = "GET";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.AllowAutoRedirect = false; HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
string encodingCodePage = //得到服务器响应内容的编码的编码页代码
Common.Common.GetCharsetFromResponseContentType(myHttpWebResponse.ContentType);
if (!Common.Common.IsValidCodePage(encodingCodePage)) encodingCodePage = "gb2312"; nextUrl = myHttpWebResponse.Headers.GetValues("Location")[0] ; myHttpWebRequest = (HttpWebRequest)WebRequest.Create(nextUrl);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.AllowAutoRedirect = false; myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); nextUrl = myHttpWebResponse.Headers.GetValues("Location")[0]; myHttpWebRequest = (HttpWebRequest)WebRequest.Create(nextUrl);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.Headers.Add(cookieStr);
myHttpWebRequest.AllowAutoRedirect = true; myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); string input = "";
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(encodingCodePage)); input = myStreamReader.ReadToEnd(); myStreamReader.Close();
myResponseStream.Close(); int start = input.IndexOf("<form action=\"ServiceLoginAuth\"");
int end = input.IndexOf("</form>", start);
string temp = input.Substring(start, end + 7 - start); temp = temp.Replace("\n", ""); //获得登陆页面的表单元素的键/值对
List<string> names = new List<string>();//键名
List<string> values = new List<string>();//值
Regex rg1 = new Regex("<(input|INPUT)(\\S|\\s)+?>");
MatchCollection matches = rg1.Matches(temp);
foreach (Match match in matches)
{
string str = match.Value;
if (str.IndexOf("name") == -1 || str.IndexOf("value") == -1) continue;
int i = str.IndexOf("name");
int k = str.IndexOf(" ", i);
if (k == -1) k = str.IndexOf(">", i);
string t1 = str.Substring(i, k - i);
string[] t2 = t1.Split('=');
string name = t2[1].Replace("\"", "");
names.Add(name); i = str.IndexOf("value");
k = str.IndexOf(" ", i);
if (k == -1) k = str.IndexOf(">", i);
t1 = str.Substring(i, k - i);
if (name != ".done")
{
t2 = t1.Split('=');
values.Add(t2[1].Replace("\"", ""));
}
else
{
int x = t1.IndexOf("=");
values.Add(t1.Substring(x + 1).Replace("\"", ""));
}
}
string result = "";
string Continue = "";
for (int j = 0; j < values.Count; j++)
{
if (names[j] == "continue") values[j] = "http://mail.google.com/mail?";
if (j == 0) result += names[j] + "=" + values[j];
else result += "&" + names[j] + "=" + values[j];
}
result += "&" + "Email=" + "shilei10";
result += "&" + "Passwd=" + "poorman";
return result;
}
首先的方法是获取登陆界面的表单元素的名和值,以生成请求数据:
private string GetQueryString()
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://mail.google.com");
myHttpWebRequest.Method = "GET";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.AllowAutoRedirect = false; HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
string encodingCodePage = //得到服务器响应内容的编码的编码页代码
Common.Common.GetCharsetFromResponseContentType(myHttpWebResponse.ContentType);
if (!Common.Common.IsValidCodePage(encodingCodePage)) encodingCodePage = "gb2312"; nextUrl = myHttpWebResponse.Headers.GetValues("Location")[0] ; myHttpWebRequest = (HttpWebRequest)WebRequest.Create(nextUrl);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.AllowAutoRedirect = false; myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); nextUrl = myHttpWebResponse.Headers.GetValues("Location")[0]; myHttpWebRequest = (HttpWebRequest)WebRequest.Create(nextUrl);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.Headers.Add(cookieStr);
myHttpWebRequest.AllowAutoRedirect = true; myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); string input = "";
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(encodingCodePage)); input = myStreamReader.ReadToEnd(); myStreamReader.Close();
myResponseStream.Close(); int start = input.IndexOf("<form action=\"ServiceLoginAuth\"");
int end = input.IndexOf("</form>", start);
string temp = input.Substring(start, end + 7 - start); temp = temp.Replace("\n", ""); //获得登陆页面的表单元素的键/值对
List<string> names = new List<string>();//键名
List<string> values = new List<string>();//值
Regex rg1 = new Regex("<(input|INPUT)(\\S|\\s)+?>");
MatchCollection matches = rg1.Matches(temp);
foreach (Match match in matches)
{
string str = match.Value;
if (str.IndexOf("name") == -1 || str.IndexOf("value") == -1) continue;
int i = str.IndexOf("name");
int k = str.IndexOf(" ", i);
if (k == -1) k = str.IndexOf(">", i);
string t1 = str.Substring(i, k - i);
string[] t2 = t1.Split('=');
string name = t2[1].Replace("\"", "");
names.Add(name); i = str.IndexOf("value");
k = str.IndexOf(" ", i);
if (k == -1) k = str.IndexOf(">", i);
t1 = str.Substring(i, k - i);
if (name != ".done")
{
t2 = t1.Split('=');
values.Add(t2[1].Replace("\"", ""));
}
else
{
int x = t1.IndexOf("=");
values.Add(t1.Substring(x + 1).Replace("\"", ""));
}
}
string result = "";
string Continue = "";
for (int j = 0; j < values.Count; j++)
{
if (names[j] == "continue") values[j] = "http://mail.google.com/mail?";
if (j == 0) result += names[j] + "=" + values[j];
else result += "&" + names[j] + "=" + values[j];
}
result += "&" + "Email=" + "shilei10";
result += "&" + "Passwd=" + "poorman";
return result;
}
private void InitializeLoginWithUsernameAndPassword()
{
string query = this.GetQueryString();
string postUri = "https://www.google.com/accounts/ServiceLoginAuth";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(postUri);
myHttpWebRequest.Method = "POST";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.ContentLength = query.Length;
myHttpWebRequest.Referer = nextUrl;
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Headers.Add(cookieStr);
myHttpWebRequest.AllowAutoRedirect = true ; HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//获得有效COOKIE
string[] setCookies = myHttpWebResponse.Headers.GetValues("Set-Cookie");
for (int m = 0; m < setCookies.Length; m++)
{
string[] t = setCookies[m].Split(';');
for (int n = 0; n < t.Length; n++)
{
if (t[n].IndexOf("expires=") > -1
|| t[n].IndexOf("path=") > -1
|| t[n].IndexOf("domain=") > -1
|| t[n].IndexOf("=") == -1)
continue;
else
{
if (cookieStr == "") cookieStr += t[n];
else
cookieStr += "; " + t[n];
}
}
}
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(nextUrl);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.Headers.Add(cookieStr);
myHttpWebRequest.AllowAutoRedirect = true; myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
nextUrl = myHttpWebResponse.ResponseUri.ToString();
}
我现在的问题就是得到完整的提交数据,难道除了表单元素的名/值之外,还有一些数据要提交给服务器?哪位给个正解啊!!!
httpheader
{
return true;
};
该怎么做呢?你的是一个函数啊,怎么加?
{
string query = this.GetQueryString();
string postUri = "https://www.google.com/accounts/ServiceLoginAuth";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(postUri);
myHttpWebRequest.Method = "POST";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.ContentLength = query.Length;
myHttpWebRequest.Referer = nextUrl;
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Headers.Add(cookieStr);
myHttpWebRequest.AllowAutoRedirect = true ; /* 加在这里 */
System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
/*********************/ HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//获得有效COOKIE
string[] setCookies = myHttpWebResponse.Headers.GetValues("Set-Cookie");
for (int m = 0; m < setCookies.Length; m++)
{
string[] t = setCookies[m].Split(';');
for (int n = 0; n < t.Length; n++)
{
if (t[n].IndexOf("expires=") > -1
|| t[n].IndexOf("path=") > -1
|| t[n].IndexOf("domain=") > -1
|| t[n].IndexOf("=") == -1)
continue;
else
{
if (cookieStr == "") cookieStr += t[n];
else
cookieStr += "; " + t[n];
}
}
}
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(nextUrl);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.Headers.Add(cookieStr);
myHttpWebRequest.AllowAutoRedirect = true; myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
nextUrl = myHttpWebResponse.ResponseUri.ToString();
}
我那样试过了,还是没有用啊,能不能帮我分析一下GMAIL登陆啊?呵呵,麻烦你了,是不是我的登陆过程中有什么数据没有得到啊.
可不可以讲得详细一点儿啊?谢谢
前段时间做警务通项目,大多请求都是HTTPS和带数字证书认证登录的。找偶吧。呵呵。