读书人

C#网络编程

发布时间: 2012-01-10 21:26:51 作者: rapoo

C#网络编程高手请进
我现在想做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;
}


[解决办法]
学习
看LX正解
[解决办法]
学习
看LX正解
[解决办法]
学习
看LX正解

[解决办法]
通不过服务器验证是安全问题吧?
[解决办法]
学习中
[解决办法]
用抓包工具看看吧
httpheader
[解决办法]
在你所有的HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();之前加上下面一条语句:

System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
[解决办法]
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 ;

/* 加在这里 */
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();
}

[解决办法]
http://www.cnblogs.com/Ring1981/archive/2006/11/09/450744.aspx

读书人网 >C#

热点推荐