程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> HttpWebRequest後台讀取網頁類

HttpWebRequest後台讀取網頁類

編輯:C#基礎知識


using System;
using System.Linq;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;

/// <summary>
/// 後台讀取網頁類,使用WebRequest對象
/// </summary>
public class RemoteWeb
{

/// <summary>
/// WebRequestPOST方式讀取遠程數據
/// </summary>
/// <param name="_url">遠程請求地址,需要加參數。</param>
/// <param name="_remoteEncoding">遠程編碼</param>
/// <returns>編碼為UTF-8的字符串</returns>
public static string Post(string _url, Encoding _remoteEncoding)
{

// 發送數據
Encoding utf8 = System.Text.Encoding.UTF8;

byte[] a = utf8.GetBytes(_url.Split('?')[1]);
byte[] data = Encoding.Convert(utf8, _remoteEncoding, a);
if (_remoteEncoding == System.Text.Encoding.UTF8)
{
data = null;
data = a;
}
//byte[] data = _remoteEncoding.GetBytes(_url.Split('?')[1]);

//System.IO.File.WriteAllText(@"e:\temp\ali2.txt", utf8.GetString(data));

Uri url = new Uri(_url.Split('?')[0]);
HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
//設置超時

/* 財付通證書 */
//X509Store store = new X509Store("Root", StoreLocation.LocalMachine);
//store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
//X509Certificate2 cer = store.Certificates.Find(X509FindType.FindBySubjectName, "1204816901", false)[0];


//驗證服務器證書回調自動驗證 SSL
if (_url.Split('?')[0].Substring(0, 5) == "https")
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteWeb.CheckValidationResult);
}
// 證書
if (_url.Split('?')[0].Substring(0, 5) == "https" && _url.Split('?')[0].IndexOf("tenpay.com") != -1)
{
req.ClientCertificates.Add(new X509Certificate2("e:/webroot/cer/1204816901_20100716110715.pfx", "1204816901"));
}

req.Timeout = 30000;
req.AllowAutoRedirect = true;
req.KeepAlive = true;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
//req.EnableSsl = true;
Stream stream = req.GetRequestStream();

stream.Write(data, 0, data.Length);
stream.Close();

HttpWebResponse rep = req.GetResponse() as HttpWebResponse;
Stream receiveStream = rep.GetResponseStream();// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, _remoteEncoding);


Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
StringBuilder sb = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append(readstr);
count = readStream.Read(read, 0, 256);
}

rep.Close();
receiveStream.Close();
readStream.Close();
req = null;
stream = null;
receiveStream = null;

byte[] c = _remoteEncoding.GetBytes(sb.ToString());
byte[] d = Encoding.Convert(_remoteEncoding, utf8, c);
return utf8.GetString(d);
}

/// <summary>
/// WebRequestPOST方式讀取遠程數據(GB2312專用)
/// </summary>
/// <param name="_url">遠程請求地址,需要加參數。</param>
/// <returns>編碼為UTF-8的字符串</returns>
public static string PostGB2312(string _url)
{

// 發送數據
Encoding utf8 = System.Text.Encoding.UTF8;

byte[] data = System.Text.Encoding.GetEncoding("gb2312").GetBytes(_url.Split('?')[1]);

//System.IO.File.WriteAllText(@"e:\temp\ali2.txt", utf8.GetString(data));

Uri url = new Uri(_url.Split('?')[0]);
HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
//設置超時

/* 財付通證書 */
//X509Store store = new X509Store("Root", StoreLocation.LocalMachine);
//store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
//X509Certificate2 cer = store.Certificates.Find(X509FindType.FindBySubjectName, "1204816901", false)[0];


//驗證服務器證書回調自動驗證 SSL
if (_url.Split('?')[0].Substring(0, 5) == "https")
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteWeb.CheckValidationResult);
}

if (_url.Split('?')[0].Substring(0, 5) == "https" && _url.Split('?')[0].IndexOf("tenpay.com") != -1)
{
req.ClientCertificates.Add(new X509Certificate2("e:/webroot/cer/1204816901_20100716110715.pfx", "1204816901"));
}

req.Timeout = 30000;
req.AllowAutoRedirect = true;
req.KeepAlive = true;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
//req.EnableSsl = true;
Stream stream = req.GetRequestStream();

stream.Write(data, 0, data.Length);
stream.Close();

HttpWebResponse rep = req.GetResponse() as HttpWebResponse;
Stream receiveStream = rep.GetResponseStream();// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.GetEncoding("gb2312"));


Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
StringBuilder sb = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append(readstr);
count = readStream.Read(read, 0, 256);
}

rep.Close();
receiveStream.Close();
readStream.Close();
req = null;
stream = null;
receiveStream = null;

byte[] c = System.Text.Encoding.GetEncoding("gb2312").GetBytes(sb.ToString());
byte[] d = Encoding.Convert(System.Text.Encoding.GetEncoding("gb2312"), utf8, c);
return utf8.GetString(d);
}

/// <summary>
/// WebRequestPOST方式讀取遠程數據(GBK專用)
/// </summary>
/// <param name="_url">遠程請求地址,需要加參數。</param>
/// <returns>編碼為UTF-8的字符串</returns>
public static string PostGBK(string _url)
{

// 發送數據
Encoding utf8 = System.Text.Encoding.UTF8;

byte[] data = System.Text.Encoding.GetEncoding("GBK").GetBytes(_url.Split('?')[1]);

//System.IO.File.WriteAllText(@"e:\temp\ali2.txt", utf8.GetString(data));

Uri url = new Uri(_url.Split('?')[0]);
HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
//設置超時

/* 財付通證書 */
//X509Store store = new X509Store("Root", StoreLocation.LocalMachine);
//store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
//X509Certificate2 cer = store.Certificates.Find(X509FindType.FindBySubjectName, "1204816901", false)[0];


//驗證服務器證書回調自動驗證 SSL
if (_url.Split('?')[0].Substring(0, 5) == "https")
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteWeb.CheckValidationResult);
}

if (_url.Split('?')[0].Substring(0, 5) == "https" && _url.Split('?')[0].IndexOf("tenpay.com") != -1)
{
req.ClientCertificates.Add(new X509Certificate2("e:/webroot/cer/1204816901_20100716110715.pfx", "1204816901"));
}

req.Timeout = 30000;
req.AllowAutoRedirect = true;
req.KeepAlive = true;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
//req.EnableSsl = true;
Stream stream = req.GetRequestStream();

stream.Write(data, 0, data.Length);
stream.Close();

HttpWebResponse rep = req.GetResponse() as HttpWebResponse;
Stream receiveStream = rep.GetResponseStream();// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.GetEncoding("gb2312"));


Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
StringBuilder sb = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append(readstr);
count = readStream.Read(read, 0, 256);
}

rep.Close();
receiveStream.Close();
readStream.Close();
req = null;
stream = null;
receiveStream = null;

byte[] c = System.Text.Encoding.GetEncoding("GBK").GetBytes(sb.ToString());
byte[] d = Encoding.Convert(System.Text.Encoding.GetEncoding("GBK"), utf8, c);
return utf8.GetString(d);
}

public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // Always accept
return true;
}

/// <summary>
/// WebRequestGET方式讀取遠程數據,默認超時30秒
/// </summary>
/// <param name="_url">遠程請求地址,需要加參數。無參數可隨意加一個。</param>
/// <param name="_remoteEncoding">遠程編碼</param>
/// <returns>編碼為UTF-8的字符串</returns>
public static string GET(string _url, Encoding _remoteEncoding)
{
return GET(_url, _remoteEncoding, 30000);
}

/// <summary>
/// WebRequestGET方式讀取遠程數據,自定超時時間
/// </summary>
/// <param name="_url">遠程請求地址,需要加參數。無參數可隨意加一個。</param>
/// <param name="_remoteEncoding">遠程編碼</param>
/// <param name="_Timeout">超時秒數</param>
/// <returns>編碼為UTF-8的字符串</returns>
public static string GET(string _url, Encoding _remoteEncoding, int _Timeout)
{
Encoding utf8 = System.Text.Encoding.UTF8;

//設置超時
HttpWebRequest req;
req = (HttpWebRequest)WebRequest.Create(_url);

req.Timeout = _Timeout;
req.Method = "GET";
req.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
Stream receiveStream = rep.GetResponseStream();// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, _remoteEncoding);

Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
//Common.WriteTXT(@"e:\webroot\errors\length.txt", readStream.ReadToEnd().ToString());
StringBuilder sb = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append(readstr);
count = readStream.Read(read, 0, 256);
}

rep.Close();
receiveStream.Close();
readStream.Close();
req = null;
receiveStream = null;

byte[] c = _remoteEncoding.GetBytes(sb.ToString());
byte[] d = Encoding.Convert(_remoteEncoding, utf8, c);
return utf8.GetString(d);
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved