博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用HttpWebRequest模拟表单提交
阅读量:5130 次
发布时间:2019-06-13

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

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         ///  12         /// 获取指定路径数据 13         ///  14         /// 提交路径 15         /// Cookie容器对象 16         /// 
字符串结果
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 /// 30 /// 默认表单提交 31 /// 32 /// 提交路径 33 /// 提交数据 34 /// Cookie容器对象 35 ///
字符串结果
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 /// 59 /// 多部件表单提交 60 /// 61 /// 提交路径 62 /// 提交数据.注:如果是文件路径,代表是文件. 63 /// Cookie容器对象 64 ///
字符串结果
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 }

 

转载于:https://www.cnblogs.com/allyn/p/10177888.html

你可能感兴趣的文章
浅谈 unix, linux, ios, android 区别和联系
查看>>
51nod 1428 活动安排问题 (贪心+优先队列)
查看>>
Solaris11修改主机名
查看>>
latex for wordpress(一)
查看>>
如何在maven工程中加载oracle驱动
查看>>
Flask 系列之 SQLAlchemy
查看>>
aboutMe
查看>>
【Debug】IAR在线调试时报错,Warning: Stack pointer is setup to incorrect alignmentStack,芯片使用STM32F103ZET6...
查看>>
一句话说清分布式锁,进程锁,线程锁
查看>>
FastDFS使用
查看>>
服务器解析请求的基本原理
查看>>
[HDU3683 Gomoku]
查看>>
下一代操作系统与软件
查看>>
[NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)
查看>>
Python IO模型
查看>>
DataGridView的行的字体颜色变化
查看>>
局域网内手机访问电脑网站注意几点
查看>>
[Serializable]的应用--注册码的生成,加密和验证
查看>>
Android-多线程AsyncTask
查看>>
LeetCode【709. 转换成小写字母】
查看>>