博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C#基础】HTTP发送POST二进制数据
阅读量:7056 次
发布时间:2019-06-28

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

//postdata为数组的请求方式public byte[] POST(string Url, byte[] byteRequest)        {            byte[] responsebody;            HttpWebRequest httpWebRequest = null;            HttpWebResponse httpWebResponse = null;            try            {                //如果是发送HTTPS请求                if (Url.StartsWith("https", StringComparison.OrdinalIgnoreCase))                {                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);                    httpWebRequest.ProtocolVersion = HttpVersion.Version10;                }                else                {                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);//创建连接请求                }                httpWebRequest.Method = "POST";                if (cookieContainer != null)                {                    httpWebRequest.CookieContainer = cookieContainer;                }                httpWebRequest.AllowAutoRedirect = AllowAutoRedirect;//【注意】这里有个时候在特殊情况下要设置为否,否则会造成cookie丢失                httpWebRequest.ContentType = ContentType;                httpWebRequest.Accept = Accept;                httpWebRequest.UserAgent = UserAgent;                if (!string.IsNullOrEmpty(uuid))                {                    httpWebRequest.Headers.Add("seed:" + uuid + "");                }                //Post请求数据,则写入传的PostData                //byte[] byteRequest = Encoding.Default.GetBytes(PostData);                httpWebRequest.ContentLength = byteRequest.Length;                using (Stream stream = httpWebRequest.GetRequestStream())                {                    stream.Write(byteRequest, 0, byteRequest.Length);                }                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//开始获取响应流                Stream responseStream = httpWebResponse.GetResponseStream();                responsebody = StreamToBytes(responseStream);                responseStream.Close();                httpWebRequest.Abort();                cookieContainer.Add(httpWebResponse.Cookies);                cookieCollection.Add(httpWebResponse.Cookies);                httpWebResponse.Close();                //到这里为止,所有的对象都要释放掉,以免内存像滚雪球一样            }            catch (Exception ex)            {                responsebody = Encoding.Default.GetBytes(ex.Message + ex.Source);                LogHelper.Log.Error("POST方式请求网页异常", ex);            }            return responsebody;        }

 

//postdata为数组的请求方式public byte[] POST(string Url, byte[] byteRequest)        {            byte[] responsebody;            HttpWebRequest httpWebRequest = null;            HttpWebResponse httpWebResponse = null;            try            {                //如果是发送HTTPS请求                if (Url.StartsWith("https", StringComparison.OrdinalIgnoreCase))                {                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);                    httpWebRequest.ProtocolVersion = HttpVersion.Version10;                }                else                {                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);//创建连接请求                }                httpWebRequest.Method = "POST";                if (cookieContainer != null)                {                    httpWebRequest.CookieContainer = cookieContainer;                }                httpWebRequest.AllowAutoRedirect = AllowAutoRedirect;//【注意】这里有个时候在特殊情况下要设置为否,否则会造成cookie丢失                httpWebRequest.ContentType = ContentType;                httpWebRequest.Accept = Accept;                httpWebRequest.UserAgent = UserAgent;                if (!string.IsNullOrEmpty(uuid))                {                    httpWebRequest.Headers.Add("seed:" + uuid + "");                }                 //Post请求数据,则写入传的PostData                //byte[] byteRequest = Encoding.Default.GetBytes(PostData);                httpWebRequest.ContentLength = byteRequest.Length;                using (Stream stream = httpWebRequest.GetRequestStream())                {                    stream.Write(byteRequest, 0, byteRequest.Length);                }                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//开始获取响应流                Stream responseStream = httpWebResponse.GetResponseStream();                responsebody = StreamToBytes(responseStream);                responseStream.Close();                httpWebRequest.Abort();                cookieContainer.Add(httpWebResponse.Cookies);                cookieCollection.Add(httpWebResponse.Cookies);                httpWebResponse.Close();                //到这里为止,所有的对象都要释放掉,以免内存像滚雪球一样            }            catch (Exception ex)            {                responsebody = Encoding.Default.GetBytes(ex.Message + ex.Source);                LogHelper.Log.Error("POST方式请求网页异常", ex);            }            return responsebody;        }

 

 

转载地址:http://oygol.baihongyu.com/

你可能感兴趣的文章
caffe 入门实例1 如何调参数
查看>>
苹果创新并未终结 离开乔布斯一样优秀
查看>>
关于for...in... 和 for..of...的使用
查看>>
良序集的一节
查看>>
《常微分方程教程》例2.3.1
查看>>
一个用原生JS造的轮播图插件
查看>>
hadoop集群环境搭建-hadoop之伪分布搭建环境
查看>>
动态编译
查看>>
分享:一个基于NPOI的excel导入导出组件(强类型)
查看>>
数据结构实验之二叉树的建立与遍历
查看>>
C++基础之迭代器
查看>>
杂题 洛谷P2018 消息传递
查看>>
算法笔记 --- Radix Sort
查看>>
Jetty的配置
查看>>
scala函数等号省略
查看>>
通过AutoConfig实现Form Server配置文件的修改 【转载】
查看>>
20165324 2017-2018-2 《Java程序设计》课程总结
查看>>
8-unittest中case管理
查看>>
ExtJs XTemplate
查看>>
[转载[工具]]PLSQL使用技巧
查看>>