HttpWebRequest后台读取网页类
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);
}
}
HttpWebRequest后台读取网页类的更多相关文章
- 正则表达式相关:C# 抓取网页类(获取网页中所有信息)
类的代码: using System; using System.Data; using System.Configuration; using System.Net; using System.IO ...
- [转]正则表达式相关:C# 抓取网页类(获取网页中所有信息)
using System; using System.Data; using System.Configuration; using System.Net; using System.IO; usin ...
- 利用backgroundwork----递归读取网页源代码,并下载href链接中的文件
今天闲着没事,研究了一下在线更新程序版本的问题.也是工作中的需要,开始不知道如何下手,各种百度也没有找到自己想要的,因为我的需求比较简单,所以就自己琢磨了一下.讲讲我的需求吧.自己在IIs上发布了一个 ...
- struts2中从后台读取数据到<s:select>
看到网上好多有struts2中从后台读取数据到<s:select>的,但都 不太详细,可能是我自己理解不了吧!所以我自己做了 一个,其中可能 有很多不好的地方,望广大网友指出 结果如图 p ...
- php 读取网页源码 , 导出成txt文件, 读取xls,读取文件夹下的所有文件的文件名
<?php // 读取网页源码$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLO ...
- Ajax的post方法,模拟 从后台读取数据小demo
$(document).ready(function() { //定义一个函数 function timer() { $.post("1.json", function(data, ...
- excel读取 工具类
package cn.yongche.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOExce ...
- C#读取网页源码
#region 1.读取 网页源码 + static string ReadHtml(string urlStr,int type) /// <summary> /// 读取 网页源码 + ...
- Utils--前台调用后台接口工具类
Utils--前台调用后台接口工具类 package com.taotao.manage.httpclient; import java.io.IOException; import java.net ...
随机推荐
- Linux(10.18-10.25)学习笔记
一.学习目标 1. 了解常见的存储技术(RAM.ROM.磁盘.固态硬盘等) 2. 理解局部性原理 3. 理解缓存思想 4. 理解局部性原理和缓存思想在存储层次结构中的应用 5. 高速缓存的原理和应用 ...
- 直接修改托管堆栈中的type object pointer(类型对象指针)
都知道.NET是一个强对象类型的框架. 那么对于对象类型又是怎么确定的呢. 最初的我简单认为数据的类型就是定义时字段的类型修饰决定的(回来发现这种观点是绝对错误的) 我们知道引用对象存储在托管堆栈中, ...
- 『方案』《女友十年精华》 ORC 图片 文字识别 详解
目的需求: 2008年,遇到一本电子书 <女友十年精华> 觉得很美,想 私藏 这些文章: >网络搜索文章 —— 没有找到: >反编译程序 —— 所有文字 都是图片格式(部分文章 ...
- IT男的”幸福”生活"续4
翻来翻去,总是睡不觉.大脑口一堆问题.一个又冒出一个,没完没了.明天该怎样去进行下一步呢.. ….. 夜一下子深黑很多,窗外的公路,时而有货车通过,动不动按喇叭,而我住在二楼,真它的吵.也许她住在五楼 ...
- Linux下SVN安装配置
第一章 安装 1. 采用源文件编译安装.源文件共两个,为:subversion-1.6.1.tar.gz (subversion 源文件)subversion-deps-1.6.1.tar.gz ...
- 0505-NABCD模型、视频
1.确定选题. 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 截止日期:2016.5.6日晚10点 NABCD模型: ...
- Html-Css-iframe的自适应高度方案
先看一个示例,有两个页面,1.html通过iframe嵌入2.html,两个页面都是同域的 a.html <!DOCTYPE html> <html> <head> ...
- Java基础-final和static的区别
很多时候会容易把static和final关键字混淆,static作用于成员变量用来表示只保存一份副本,而final的作用是用来保证变量不可变.看下面这个例子: public class Test { ...
- Linux operation strucutre
Under the /usr/src directory. 1.arch目录包括了所有和体系结构相关的核心代码.它下面的每一个子目录都代表一种Linux支持的体系结构,例如i386就是Intel CP ...
- 求第N数大问题
问题: InputThe first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of ...