C#模拟Http与Https请求框架实例
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace WebRequestTest
{
/// <summary>
/// 动态类,每个实例使用单独session
/// </summary>
public class HttpHelperNew
{
public CookieContainer cookie = new CookieContainer();
/// <summary>
/// post请求返回html
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//request.AllowAutoRedirect = false; //禁止自动重定向
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
request.CookieContainer = cookie; //cookie信息由CookieContainer自行维护
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = null;
try
{
this.SetCertificatePolicy();
response = (HttpWebResponse)request.GetResponse();
}
catch (System.Exception ex)
{
}
//获取重定向地址
//string url1 = response.Headers["Location"];
if (response !=null)
{
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
else
{
return "error"; //post请求返回为空
}
}
/// <summary>
/// get请求获取返回的html
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public string HttpGet(string Url, string Querydata)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (Querydata == "" ? "" : "?") + Querydata);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.CookieContainer = cookie;
this.SetCertificatePolicy();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// response.Cookies = cookie.GetCookies(response.ResponseUri);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
/// <summary>
/// 获得响应中的图像
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public Stream GetResponseImage(string url)
{
Stream resst = null;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.KeepAlive = true;
req.Method = "GET";
req.AllowAutoRedirect = true;
req.CookieContainer = cookie;
req.ContentType = "application/x-www-form-urlencoded";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Timeout = ;
Encoding myEncoding = Encoding.GetEncoding("UTF-8");
this.SetCertificatePolicy();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
resst = res.GetResponseStream();
return resst;
}
catch
{
return null;
}
}
/// <summary>
/// 正则获取匹配的第一个值
/// </summary>
/// <param name="html"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public string getStringByRegex(string html,string pattern)
{
Regex re = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matchs = re.Matches(html);
if (matchs.Count > )
{
return matchs[].Groups[].Value;
}
else
return "";
}
/// <summary>
/// 正则验证返回的response是否正确
/// </summary>
/// <param name="html"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public bool verifyResponseHtml(string html ,string pattern)
{
Regex re = new Regex(pattern);
return re.IsMatch(html);
}
//注册证书验证回调事件,在请求之前注册
private void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback
+= RemoteCertificateValidate;
}
/// <summary>
/// 远程证书验证,固定返回true
/// </summary>
private static bool RemoteCertificateValidate(object sender, X509Certificate cert,
X509Chain chain, SslPolicyErrors error)
{
return true;
}
}
}
C#模拟Http与Https请求框架实例的更多相关文章
- 模拟http或https请求,实现ssl下的bugzilla登录、新增BUG,保持会话以及处理token
1.增加相应httpclient 需要的jar包到工程,如果是maven工程请在pom.xml增加以下配置即可: <dependency> <groupId>org.apach ...
- .net 模拟登陆 post https 请求跳转页面
AllowAutoRedirect property is true, the Referer property is set automatically when the request is re ...
- Volley框架支持HTTPS请求。
第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...
- Android网络请求框架AsyncHttpClient实例详解(配合JSON解析调用接口)
最近做项目要求使用到网络,想来想去选择了AsyncHttpClient框架开进行APP开发.在这里把我工作期间遇到的问题以及对AsyncHttpClient的使用经验做出相应总结,希望能对您的学习有所 ...
- java中模拟http(https)请求的工具类
在java中,特别是java web中,我们经常需要碰到的一个场景是我们需要从服务端去发送http请求,获取到数据,而不是直接从浏览器输入请求网址获得相应.比如我们想访问微信接口,获取其返回信息. 在 ...
- php封装curl,模拟POST和GET请求HTTPS请求
<?php /** * @title 封装代理请求 * @author victor **/ class ApiRequest { /** * curl提交数据 * @param String ...
- Google Volley框架之https请求
先插一句.Google出的volley框架本身是支持https请求的,可是仅仅是针对有第三方机构认证过的. 假设自己随便在网上搞的一个证书,那volley是不支持请求的. 本文讲下怎样让volley支 ...
- PHP-Curl模拟HTTPS请求
使用PHP-Curl方式模拟HTTPS请求,测试接口传参和返回值状态 上代码!! <?php /** * 模拟post进行url请求 * @param string $url * @par ...
- PHP: 手把手编写自己的 MVC 框架实例教程
1 什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...
随机推荐
- Android-- ImageLoader-- UIL doesn't support scheme(protocol) by default [pg].
在ImageLoader加载图片是, 地址是容易出错的,特别是本地图片: String imageUri = "http://site.com/image.png"; // fro ...
- 注解@RequestMapping 的使用
1首先@RequestMapping 中的值,我们说请求方法l路径,请求url我们都知道怎么请求了,在第一节helloworld中, 我们先说我们先建一个类,RequestMappingTest 方法 ...
- 试听笔记:javascript入门精通
一.数据类型 1.原始类型:number.string.boolean.null.undefined 2.对象类型:Object (Function.Array.Date...) P.类型隐式转换:' ...
- Django学习笔记之二
一.使用Django自带的后端管理平台 1.后台创建管理员 python manage.py createsuperuser Email address: admin@example.com Pass ...
- css 温故而知新 定位(position)与权限(z-index)
1.进行定位(position)的元素的权限(z-index)永远比没有定位的高. 2.如果两个元素都定位了,无论是相对定位还是绝对定位.他们的权限都是等权的. 3.两个相同定位的元素,除了z-ind ...
- Sublime Text 3 杂记
Sublime Text 是一个功能强大的代码编辑器(收费,但可无限期试用).由程序员Jon Skinner于2008年1月份所开发出来,它最初被设计为一个具有丰富扩展功能的Vim.Sublime T ...
- 72. 求m到n之和
求m到n之和 int sum(int m, int n) { int i, result = 0; for (i=m; i<=n; i++) result = result+i; return ...
- Rails--抛出异常
begin ... rescue Exception => e ... end
- javascript入门:this的规则与bind的各种风骚用法
javascript的任何函数的作用域中都有一个this变量.理解这个this变量才能正确地编写javascript程序,正确使用javascript的各种功能. this变量是这样确定的: 1 如果 ...
- MVC中的一些坑
1.@字符在.cshtml文件中不能启用 经过2个小时求解,发现是配置文件web.config中引用的namespace少了System.Web.Optimization,所以不能启用,因为创建的空的 ...