博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Http GetPost网络请求
阅读量:5259 次
发布时间:2019-06-14

本文共 5631 字,大约阅读时间需要 18 分钟。

using Newtonsoft.Json;using System;using System.Collections.Generic;using System.IO;using System.Net;using System.Net.Security;using System.Security.Cryptography.X509Certificates;using System.Text;namespace ZhuoHuiSchoolRoom.ZhuoHuiClass{    class HttpWebResponseUtility    {        private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";        ///          /// 创建GET方式的HTTP请求         ///          /// 请求的URL         /// 请求的超时时间         /// 请求的客户端浏览器信息,可以为空         /// 随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空         /// 
public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.UserAgent = DefaultUserAgent; if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } return request.GetResponse() as HttpWebResponse; } /// /// 创建POST方式的HTTP请求 /// /// 请求的URL /// json字符串 /// 发送HTTP请求时所用的编码 ///
public static HttpWebResponse CreatePostHttpResponse(string url, string str, Encoding requestEncoding) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } if (requestEncoding == null) { throw new ArgumentNullException("requestEncoding"); } HttpWebRequest request = null; request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/json;charset=utf-8"; request.UserAgent = DefaultUserAgent; byte[] data = requestEncoding.GetBytes(str.ToString()); //将json转为二进制流 using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } return request.GetResponse() as HttpWebResponse; } /// /// 创建DELETE方式的HTTP请求 /// /// 请求地址 ///
public static HttpWebResponse CreateDeleteHttpResponse(string url) { HttpWebRequest request = null; string urlPath = url; int millisecond = 30000; request = (HttpWebRequest)WebRequest.Create(urlPath); //request.Proxy = null;//关闭代理(重要) request.Timeout = millisecond; request.Method = "DELETE"; //request.Accept = "application/json"; //request.ContentType = "application/json"; request.ServicePoint.Expect100Continue = false; return request.GetResponse() as HttpWebResponse; } private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; //总是接受 } /// /// HttpPost上传多个文件 /// /// 请求地址 /// 路径集合 ///
public static HttpWebResponse HttpUploadFile(string url, List
pathList) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; string boundary = "----" + DateTime.Now.Ticks.ToString("x"); // 随机分隔线 request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary; byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); Stream postStream = request.GetRequestStream(); for (int i = 0; i < pathList.Count; i++) { int pos = pathList[i].LastIndexOf("\\"); string fileName = pathList[i].Substring(pos + 1); //取文件名 //请求头部信息 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName)); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); FileStream fs = new FileStream(pathList[i], FileMode.Open, FileAccess.Read); byte[] bArr = new byte[fs.Length]; fs.Read(bArr, 0, bArr.Length); fs.Close(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStream.Write(bArr, 0, bArr.Length); } postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); postStream.Close(); //发送请求并获取相应回应数据 return request.GetResponse() as HttpWebResponse; } }}

 

转载于:https://www.cnblogs.com/Mzg121584668/p/7779616.html

你可能感兴趣的文章
Java 方法实例
查看>>
Java 异常处理
查看>>
Java 目录
查看>>
Java 数据结构
查看>>
MYSQL5.7.24编译安装
查看>>
mysql启动过程
查看>>
应用Python来计算排列中的逆序数个数
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
关于拷贝构造函数与赋值构造函数的深刻解析
查看>>
Sping注解:注解和含义
查看>>
站立会议第四天
查看>>
用原生JS获取非行间样式
查看>>
toolbox类
查看>>
如何快速掌握一门技术
查看>>
利用AMPScript获取Uber用户数据的访问权限
查看>>
vagrant 同时设置多个同步目录
查看>>
python接口自动化28-requests-html爬虫框架
查看>>
爬虫学习笔记(一)初识爬虫
查看>>