public static string GetPage(string url, string encoding)
{
PublicVariables.NetworkConnection = false;
int browserTimeout = PublicVariables.BrowserTimeout;
string str = PublicVariables.Cfg_Agent;
string str2 = PublicVariables.last_url;
CookieContainer cookie = new CookieContainer();
string str3 = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "GET";
request.CookieContainer = cookie;
request.Referer = str2;
request.Timeout = browserTimeout; request.ReadWriteTimeout = browserTimeout;
request.AllowAutoRedirect = true;
request.UserAgent = str;
request.Proxy = null;
if (url.StartsWith("https://") && url.Contains(".taobao.com"))
{
request.ProtocolVersion = HttpVersion.Version10;
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (encoding == "")
str3 = new StreamReader(response.GetResponseStream(), Encoding.Default).ReadToEnd();
else
str3 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)).ReadToEnd();
str2 = url;
PublicVariables.NetworkConnection = true;
return str3;
}
catch (Exception exception)
{
PublicVariables.NetworkConnection = false;
return exception.ToString();
}
}
public static string GetPage(string url,string last_url, string encoding,ref CookieContainer cookie )
{
//PublicVariables.NetworkConnection = false;
int browserTimeout = PublicVariables.BrowserTimeout*5;
string str = PublicVariables.Cfg_Agent;
string str2 = last_url;
string str3 = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "GET";
request.CookieContainer = cookie ;
request.Referer = str2;
request.Timeout = browserTimeout;
request.ReadWriteTimeout = browserTimeout;
request.AllowAutoRedirect = true;
request.UserAgent = str;
request.Proxy = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (encoding == "")
str3 = new StreamReader(response.GetResponseStream()).ReadToEnd();
else
str3 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)).ReadToEnd();
str2 = url;
if (response.Headers == null) { }
else
{
for (int jj = 0; jj < response.Headers.Count; jj++)
{
string aa = response.Headers[jj].ToString();
}
}
if (response.Cookies == null)
{ }
else
{
for (int jj = 0; jj < response.Cookies.Count; jj++)
{
Cookie ccc = response.Cookies[jj];
string key, value;
key = ccc.Name;
value = ccc.Value;
}
}
cookie.Add(response.Cookies);
return str3;
}
catch (Exception exception)
{
return exception.ToString();
}
}
public static string GetPage(string url, string last_url, string encoding, ref CookieContainer cookie,ref HttpWebResponse rsp)
{
int browserTimeout = PublicVariables.BrowserTimeout ;
string str = PublicVariables.Cfg_Agent;
string str2 = last_url;
string str3 = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "GET";
request.CookieContainer =cookie ;
request.Referer = str2;
request.Timeout = browserTimeout * 2;
request.ReadWriteTimeout = browserTimeout*2;
request.AllowAutoRedirect = true;
request.UserAgent = str;
request.Proxy = null;
if (url.StartsWith("https://"))
{
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (encoding == "")
str3 = new StreamReader(response.GetResponseStream()).ReadToEnd();
else
str3 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)).ReadToEnd();
str2 = url;
if (response.Headers == null) { }
else
{
for (int jj = 0; jj < response.Headers.Count; jj++)
{
string aa = response.Headers[jj].ToString();
}
}
if (response.Cookies == null)
{ }
else
{
for (int jj = 0; jj < response.Cookies.Count; jj++)
{
Cookie ccc = response.Cookies[jj];
string key, value;
key = ccc.Name;
value = ccc.Value;
}
}
cookie.Add(response.Cookies);
rsp = response;
return str3;
}
catch (Exception exception)
{
return exception.ToString();
}
}
public static string GetPageToFile(string url,string last_url, string path, ref CookieContainer cookie)
{
int browserTimeout = PublicVariables.BrowserTimeout ;
string str2 = PublicVariables.Cfg_Agent;
string str3 = last_url;
//CookieContainer cookie = PublicVariables.cookie;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "GET";
request.CookieContainer = cookie;
request.Referer = str3;
request.Timeout = browserTimeout;
request.AllowAutoRedirect = true; request.Proxy = null; request.ServicePoint.Expect100Continue = false;
request.UserAgent = str2; request.ServicePoint.Expect100Continue = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
SaveBinaryFile(response, path);
str3 = url;
cookie.Add(response.Cookies);
return path;
}
catch
{
return "";
}
return "";
}
public static string PostPage(string url, string last_url, Hashtable PostCs, string encoding, ref CookieContainer cookie)
{
PublicVariables.NetworkConnection = false;
string s = "";
string str2 = "";
int browserTimeout = PublicVariables.BrowserTimeout ;
string str3 = PublicVariables.Cfg_Agent;
string str4 = last_url;
ArrayList list = new ArrayList(PostCs.Keys);
foreach (string str5 in list)
{
s = s + "&" + str5 + "=" + PostCs[str5].ToString();
}
if (s.Length > 0)
{
s = s.Remove(0, 1);
}
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
byte[] bytes = Encoding.UTF8.GetBytes(s);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.CookieContainer = cookie;
request.Referer = str4;
request.Timeout = browserTimeout;
request.ReadWriteTimeout = browserTimeout;
request.AllowAutoRedirect = true;
request.Proxy = null;
request.ServicePoint.Expect100Continue = false;
request.UserAgent = str3;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Tablet PC 2.0)";
request.Accept = "application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
if (url.StartsWith("https://"))
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2 cert;
int cc = store.Certificates.Count;
request.ClientCertificates = store.Certificates;
for (int iii = 0; iii < store.Certificates.Count; iii++)
{
cert = store.Certificates[iii];
//cert.IssuerName.Name
}
}
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader;
if (encoding == "")
reader = new StreamReader(responseStream);
else
reader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
str2 = reader.ReadToEnd();
reader.Close();
responseStream.Close();
str4 = url;
cookie.Add(response.Cookies);
IEnumerator enumerator=null ;
string str = "";
try
{
enumerator = cookie.GetCookies(new Uri(url)).GetEnumerator();
while (enumerator.MoveNext())
{
str = str +System.Runtime.CompilerServices. RuntimeHelpers.GetObjectValue(enumerator.Current).ToString() + "\r\n";
}
}
finally
{
if (enumerator is IDisposable)
{
(enumerator as IDisposable).Dispose();
}
}
return str2;
}
catch (Exception exception)
{
return exception.ToString();
}
}
public static string PostPage(string url, string last_url, string data, string encoding, ref CookieContainer cookie)
{
PublicVariables.NetworkConnection = false;
string s = "";
string str2 = "";
int browserTimeout = PublicVariables.BrowserTimeout;
string str3 = PublicVariables.Cfg_Agent;
string str4 = last_url;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
byte[] bytes = Encoding.UTF8.GetBytes(data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.CookieContainer = cookie;
request.Referer = str4;
request.Timeout = browserTimeout;
request.ReadWriteTimeout = browserTimeout;
request.AllowAutoRedirect = true;
request.Proxy = null;
request.ServicePoint.Expect100Continue = false;
request.UserAgent = str3;
request.UserAgent = str3;// "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Tablet PC 2.0)";
request.Accept = "application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
if (url.StartsWith("https://"))
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2 cert;
int cc = store.Certificates.Count;
request.ClientCertificates = store.Certificates;
for (int iii = 0; iii < store.Certificates.Count; iii++)
{
cert = store.Certificates[iii];
//cert.IssuerName.Name
}
}
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader;
if (encoding == "")
reader = new StreamReader(responseStream);
else
reader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
str2 = reader.ReadToEnd();
reader.Close();
responseStream.Close();
str4 = url;
cookie.Add(response.Cookies);
IEnumerator enumerator = null;
string str = "";
try
{
enumerator = cookie.GetCookies(new Uri(url)).GetEnumerator();
while (enumerator.MoveNext())
{
str = str + System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(enumerator.Current).ToString() + "\r\n";
}
}
finally
{
if (enumerator is IDisposable)
{
(enumerator as IDisposable).Dispose();
}
}
return str2;
}
catch (Exception exception)
{
return exception.ToString();
}
}
private static bool SaveBinaryFile(WebResponse response, string FileName)
{
bool flag = true;
byte[] buffer = new byte[0x400];
try
{
int num;
if (System.IO.File.Exists(FileName))
{
System.IO.File.Delete(FileName);
}
Stream stream = System.IO.File.Create(FileName);
Stream responseStream = response.GetResponseStream();
do
{
num = responseStream.Read(buffer, 0, buffer.Length);
if (num > 0)
{
stream.Write(buffer, 0, num);
}
}
while (num > 0);
stream.Close();
responseStream.Close();
}
catch
{
flag = false;
}
return flag;
}
原文:http://www.cnblogs.com/zhshlimi/p/5605499.html