用法

WebRequestSugar ws = new WebRequestSugar();
//可选参数
//ws.SetAccept
//ws.SetContentType
//ws.SetCookie
//ws.SetTimeOut
//ws.SetIsAllowAutoRedirect //GET
var html= ws.HttpGet("http://localhost:24587/Http/HttpTest.aspx"); //带参GET
var paras=new Dictionary<string, string>() ;
paras.Add("name","skx");
paras.Add("id", "");
var html2 = ws.HttpGet("http://localhost:24587/Http/HttpTest.aspx",paras ); //POST
var postHtml= ws.HttpPost("http://localhost:24587/Http/HttpTest.aspx", paras); //post and file
var postHtml2 = ws.HttpUploadFile("http://localhost:24587/Http/HttpTest.aspx", "文件地址可以是数组", paras);

封装类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Specialized; namespace SyntacticSugar
{ /// <summary>
/// ** 描述:模拟HTTP POST GET请求并获取数据
/// ** 创始时间:2015-11-24
/// ** 修改时间:-
/// ** 作者:sunkaixuan
/// ** 使用说明:
/// </summary>
public class WebRequestSugar
{
/// <summary>
/// 设置cookie
/// </summary>
private CookieContainer cookie; /// <summary>
/// 是否允许重定向
/// </summary>
private bool allowAutoRedirect = true; /// <summary>
/// contentType
/// </summary>
private string contentType = "application/x-www-form-urlencoded"; /// <summary>
/// accept
/// </summary>
private string accept = "*/*"; /// <summary>
/// 过期时间
/// </summary>
private int time = ; /// <summary>
/// 设置请求过期时间(单位:毫秒)(默认:5000)
/// </summary>
/// <param name="time"></param>
public void SetTimeOut(int time)
{
this.time = time;
} /// <summary>
/// 设置accept(默认:*/*)
/// </summary>
/// <param name="accept"></param>
public void SetAccept(string accept)
{
this.accept = accept;
} /// <summary>
/// 设置contentType(默认:application/x-www-form-urlencoded)
/// </summary>
/// <param name="contentType"></param>
public void SetContentType(string contentType)
{
this.contentType = contentType;
} /// <summary>
/// 设置Cookie
/// </summary>
/// <param name="cookie"></param>
public void SetCookie(CookieContainer cookie)
{
this.cookie = cookie;
}
/// <summary>
/// 是否允许重定向(默认:true)
/// </summary>
/// <param name="allowAutoRedirect"></param>
public void SetIsAllowAutoRedirect(bool allowAutoRedirect)
{
this.allowAutoRedirect = allowAutoRedirect;
} /// <summary>
/// post请求返回html
/// </summary>
/// <param name="url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public string HttpPost(string url, Dictionary<string, string> postdata)
{
string postDataStr = null;
if (postdata != null && postdata.Count > )
{
postDataStr = string.Join("&", postdata.Select(it => it.Key + "=" + it.Value));
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = allowAutoRedirect;
request.Method = "POST";
request.Accept = accept;
request.ContentType = this.contentType;
request.Timeout = time;
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
if (cookie != null)
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)
{
throw 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 null; //post请求返回为空
}
}
/// <summary>
/// get请求获取返回的html
/// </summary>
/// <param name="url">无参URL</param>
/// <param name="querydata">参数</param>
/// <returns></returns>
public string HttpGet(string url, Dictionary<string, string> querydata)
{
if (querydata != null && querydata.Count > )
{
url += "?" + string.Join("&", querydata.Select(it => it.Key + "=" + it.Value));
} return HttpGet(url);
}
/// <summary>
/// get请求获取返回的html
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public string HttpGet(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.CookieContainer = cookie;
request.Accept = this.accept;
request.Timeout = time;
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>
/// POST文件
/// </summary>
/// <param name="url"></param>
/// <param name="file">文件路径</param>
/// <param name="postdata"></param>
/// <returns></returns>
public string HttpUploadFile(string url, string file, Dictionary<string, string> postdata)
{
return HttpUploadFile(url, file, postdata, Encoding.UTF8);
}
/// <summary>
/// POST文件
/// </summary>
/// <param name="url"></param>
/// <param name="file">文件路径</param>
/// <param name="postdata">参数</param>
/// <param name="encoding"></param>
/// <returns></returns>
public string HttpUploadFile(string url, string file, Dictionary<string, string> postdata, Encoding encoding)
{
return HttpUploadFile(url, new string[] { file }, postdata, encoding);
}
/// <summary>
/// POST文件
/// </summary>
/// <param name="url"></param>
/// <param name="files">文件路径</param>
/// <param name="postdata">参数</param>
/// <returns></returns>
public string HttpUploadFile(string url, string[] files, Dictionary<string, string> postdata)
{
return HttpUploadFile(url, files, postdata, Encoding.UTF8);
}
/// <summary>
/// POST文件
/// </summary>
/// <param name="url"></param>
/// <param name="files">文件路径</param>
/// <param name="postdata">参数</param>
/// <param name="encoding"></param>
/// <returns></returns>
public string HttpUploadFile(string url, string[] files, Dictionary<string, string> postdata, Encoding encoding)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); //1.HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.KeepAlive = true;
request.Accept = this.accept;
request.Timeout = this.time;
request.AllowAutoRedirect = this.allowAutoRedirect;
if (cookie != null)
request.CookieContainer = cookie;
request.Credentials = CredentialCache.DefaultCredentials; using (Stream stream = request.GetRequestStream())
{
//1.1 key/value
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
if (postdata != null)
{
foreach (string key in postdata.Keys)
{
stream.Write(boundarybytes, , boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, postdata[key]);
byte[] formitembytes = encoding.GetBytes(formitem);
stream.Write(formitembytes, , formitembytes.Length);
}
} //1.2 file
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
byte[] buffer = new byte[];
int bytesRead = ;
for (int i = ; i < files.Length; i++)
{
stream.Write(boundarybytes, , boundarybytes.Length);
string header = string.Format(headerTemplate, "file" + i, Path.GetFileName(files[i]));
byte[] headerbytes = encoding.GetBytes(header);
stream.Write(headerbytes, , headerbytes.Length);
using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
{
while ((bytesRead = fileStream.Read(buffer, , buffer.Length)) != )
{
stream.Write(buffer, , bytesRead);
}
}
} //1.3 form end
stream.Write(endbytes, , endbytes.Length);
}
//2.WebResponse
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
return stream.ReadToEnd();
}
} /// <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 = allowAutoRedirect;
req.CookieContainer = cookie;
req.ContentType = this.contentType;
req.Accept = this.accept;
req.Timeout = time;
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>
private 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>
private 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;
}
}
}

WebRequestSugar的更多相关文章

  1. ASP.NET HTTP模拟提交通用类 GET POST

     用法: WebRequestSugar ws = new WebRequestSugar(); //可选参数 //ws.SetAccept //ws.SetContentType //ws.SetC ...

随机推荐

  1. supervisord 使用记录

    #supervisor简介 Supervisor是一个 Python 开发的 client/server 系统,可以管理和监控类 UNIX 操作系统上面的进程. #组成部分 supervisord(s ...

  2. 5.26 idea生成javadoc

  3. animation与transition的简单讲述

    CSS动画分为两大组成部分:transition和animation 在CSS 3引入Transition(过渡)这个概念之前,CSS是没有时间轴的.也就是说,所有的状态变化,都是即时完成. tran ...

  4. iconfont在ios(safari)中的坑

    最近公司决定将项目图标整体迁移到iconfont,按网上常规方法,在安卓.pc端都没问题,唯独在ios的safari浏览器及微信内置浏览器中,iconfont始终在正常位置向下偏移,导致图标错乱. 网 ...

  5. Oracle存储过程给变量赋值的方法

    截止到目前我发现有三种方法可以在存储过程中给变量进行赋值: 1.直接法     := 如:v_flag := 0; 2.select into 如:假设变量名为v_flag,select count( ...

  6. Caffe+Kubuntu16.04_X64+CUDA 8.0配置

    前言: 经过尝试过几次Caffe,theano,MxNet之后,很长时间没有进行caffe的更新,此次在Ubuntu16.04下安装Caffe,折腾了一天时间,终于安装成功. 参考链接:Caffe+U ...

  7. 安卓代码迁移:Make.exe: *** [***.o]Error 1

    描述:NDK开发中显示,windows环境下NDK开发 解决办法:查找系统环境变量,找到关于Cygwin的环境变量或其他无效的环境变量删除处理.

  8. 读书笔记「Python编程:从入门到实践」_5.if语句

    5.1 一个简单示例 cars = ['audi', 'bmw', 'subaru', 'toyota'] for car in cars: if car == 'bmw': print(car.up ...

  9. Pjax无刷新跳转页面实现,支持超链接与表单提交

    什么是pjax? 当你点击一个站内的链接的时候,不是做页面跳转,而是只是站内页面刷新.这样的用户体验,比起整个页面都闪一下来说, 好很多. 其中有一个很重要的组成部分, 这些网站的ajax刷新是支持浏 ...

  10. SqlLite提高批量插入速度的方法及原因分析

    (1)-SQLite忽略大小写查询大部分数据库在进行字符串比较时,对大小写是不敏感的.但是SQLite却是大小写敏感的.如果想让SQLite忽略大小写,方法如下:方法一:使用大小写转换函数LOWER. ...