当前位置:
首页 > Python基础教程 >
-
C#教程之利用HttpWebRequest模拟表单提交
1 using System; 2 using System.Collections.Specialized; 3 using System.IO; 4 using System.Net; 5 using System.Text; 6 7 namespace Allyn.Common 8 { 9 public class HttpHelper 10 { 11 /// <summary> 12 /// 获取指定路径数据 13 /// </summary> 14 /// <param name="requestUri">提交路径</param> 15 /// <param name="cookie">Cookie容器对象</param> 16 /// <returns>字符串结果</returns> 17 public static string GetForm(string requestUri, CookieContainer cookie) 18 { 19 HttpWebRequest request = WebRequest.CreateHttp(requestUri); 20 request.Method = "get"; 21 request.CookieContainer = cookie; 22 request.ContentLength = 0; 23 24 WebResponse response = request.GetResponse(); 25 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); 26 return reader.ReadToEnd(); 27 } 28 29 /// <summary> 30 /// 默认表单提交 31 /// </summary> 32 /// <param name="requestUri">提交路径</param> 33 /// <param name="postData">提交数据</param> 34 /// <param name="cookie">Cookie容器对象</param> 35 /// <returns>字符串结果</returns> 36 public static string PostForm(string requestUri, NameValueCollection postData, CookieContainer cookie) 37 { 38 HttpWebRequest request = WebRequest.CreateHttp(requestUri); 39 request.Method = "post"; 40 request.ContentType = "application/x-www-form-urlencoded"; 41 request.CookieContainer = cookie; 42 43 StringBuilder stringBuilder = new StringBuilder(); 44 foreach (string key in postData.Keys) 45 { 46 stringBuilder.AppendFormat("&{0}={1}", key, postData.Get(key)); 47 } 48 byte[] buffer = Encoding.UTF8.GetBytes(stringBuilder.ToString().Trim('&')); 49 Stream requestStream = request.GetRequestStream(); 50 requestStream.Write(buffer, 0, buffer.Length); 51 requestStream.Close(); 52 53 WebResponse response = request.GetResponse(); 54 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); 55 return reader.ReadToEnd(); 56 } 57 58 /// <summary> 59 /// 多部件表单提交 60 /// </summary> 61 /// <param name="requestUri">提交路径</param> 62 /// <param name="postData">提交数据.注:如果是文件路径,代表是文件.</param> 63 /// <param name="cookie">Cookie容器对象</param> 64 /// <returns>字符串结果</returns> 65 public static string PostFormMultipart(string requestUri, NameValueCollection postData, CookieContainer cookie) 66 { 67 string boundary = string.Format("-----{0}", DateTime.Now.Ticks.ToString("x")); 68 HttpWebRequest webrequest = WebRequest.CreateHttp(requestUri); 69 webrequest.CookieContainer = cookie; 70 webrequest.Timeout = 120000; 71 webrequest.Method = "post"; 72 webrequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); 73 74 Stream requestStream = webrequest.GetRequestStream(); 75 foreach (string key in postData.Keys) 76 { 77 StringBuilder strBuilder = new StringBuilder(); 78 strBuilder.AppendFormat("--{0}", boundary); 79 strBuilder.AppendFormat("\r\nContent-Disposition: form-data; name=\"{0}\"", key); 80 if (File.Exists(postData.Get(key))) 81 { 82 strBuilder.AppendFormat(";filename=\"{0}\"\r\nContent-Type: multipart/form-data\r\n\r\n", Path.GetFileName(postData.Get(key))); 83 byte[] buffer = Encoding.UTF8.GetBytes(strBuilder.ToString()); 84 requestStream.Write(buffer, 0, buffer.Length); 85 //获取图片流 86 FileStream fileStream = new FileStream(postData.Get(key), FileMode.Open, FileAccess.Read); 87 BinaryReader binaryReader = new BinaryReader(fileStream); 88 byte[] fileBuffer = binaryReader.ReadBytes((int)fileStream.Length); 89 binaryReader.Close(); 90 fileStream.Close(); 91 requestStream.Write(fileBuffer, 0, fileBuffer.Length); 92 } 93 else 94 { 95 strBuilder.AppendFormat("\r\n\r\n{0}\r\n", postData.Get(key)); 96 byte[] buff = Encoding.UTF8.GetBytes(strBuilder.ToString()); 97 requestStream.Write(buff, 0, buff.Length); 98 } 99 } 100 101 byte[] boundaryBuffer = Encoding.UTF8.GetBytes(string.Format("\r\n--{0}\r\n", boundary)); 102 requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length); 103 requestStream.Close(); 104 105 WebResponse response = webrequest.GetResponse(); 106 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); 107 return reader.ReadToEnd(); 108 } 109 } 110 }
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式