博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[工具类]模拟请求时提交大字符串
阅读量:6967 次
发布时间:2019-06-27

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

写在前面

开发中也会经常用到模拟请求的东东,有时候提交的数据比较大,一般的方式就不行了,这个时候,下面的方式就会更好的解决你的问题。

方法

提交的数据比较大的时候,就会用到这个方法,当然可以对该方法进行修改一下,也可以提交文件。

///         /// 提交大数据量        ///         ///         ///         /// 
public static string PostBigString(string url, string postData) { string responseContent; var memStream = new MemoryStream(); var webRequest = (HttpWebRequest)WebRequest.Create(url); // 边界符 var boundary = "---------------" + DateTime.Now.Ticks.ToString("x"); // 边界符 var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); //var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); // 最后的结束符 var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n"); // 设置属性 webRequest.Method = "POST"; webRequest.Timeout = 40000; webRequest.ContentType = "multipart/form-data; boundary=" + boundary; // 写入文件 const string filePartHeader = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n"; var header = string.Format(filePartHeader, "", ""); var headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(beginBoundary, 0, beginBoundary.Length); memStream.Write(headerbytes, 0, headerbytes.Length); var buffer = Encoding.UTF8.GetBytes(postData); // 写入字符串的Key var stringKeyHeader = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"" + "\r\n\r\n{1}\r\n"; string formitem = string.Format(stringKeyHeader, "value", postData); byte[] formitembytes = Encoding.UTF8.GetBytes(formitem); memStream.Write(formitembytes, 0, formitembytes.Length); // 写入最后的结束边界符 memStream.Write(endBoundary, 0, endBoundary.Length); webRequest.ContentLength = memStream.Length; var requestStream = webRequest.GetRequestStream(); memStream.Position = 0; var tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); memStream.Close(); requestStream.Write(tempBuffer, 0, tempBuffer.Length); //requestStream.Close(); //加入这行会报错! var httpWebResponse = (HttpWebResponse)webRequest.GetResponse(); using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"))) { responseContent = httpStreamReader.ReadToEnd(); } httpWebResponse.Close(); webRequest.Abort(); return responseContent; }

普通的模拟post请求的方法

///         /// post请求        ///         /// 请求的地址        /// 参数        /// 
public static string PostString(string url, string postdata) { string strJson = string.Empty; try { byte[] buffer = Encoding.UTF8.GetBytes(postdata); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json;charset:utf-8"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = buffer.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(buffer, 0, buffer.Length); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { strJson = sr.ReadToEnd(); } } return strJson; } catch (Exception ex) { throw ex; } }

对于一般的post请求,上面的方法已经能解决了,但是对应提交的数据比较大的时候,上面的这个方法就会显得力不从心了。

总结

在项目中,经常用到模拟请求某个接口的情况,不想每次都去f12查看请求头的东西,这也算总结一下,方便以后的使用。

转载于:https://www.cnblogs.com/wolf-sun/p/4977625.html

你可能感兴趣的文章
powershell 批量查询导出 组织内OU
查看>>
我的友情链接
查看>>
昨日终于考完路考了
查看>>
转:解决 linux下编译make文件报错“/bin/bash^M: 坏的解释器:没有那个文件或目录...
查看>>
Discuz 7.2坑爹集锦-PHP篇 update 20120525
查看>>
IDEA 2016.3 导入项目
查看>>
spring4+hibernate4+struts2注解,class找不到bean
查看>>
java中synchronized和Lock的区别
查看>>
python练习-数值比较
查看>>
win7,centos双系统无法启动centos,跳到grub命令行
查看>>
我的友情链接
查看>>
堆和栈的区别
查看>>
MongoDB基本命令用法
查看>>
MongoDB基本命令用法
查看>>
Docker的四种网络方式
查看>>
【Qt笔记】可视化显示数据库数据
查看>>
Spring Boot 2.0正式发布,升还是不升呢?
查看>>
Android四大组件Activity,及Intent的简单用法
查看>>
线程同步控制
查看>>
正则表达式
查看>>