【C#基础】HTTP发送POST二进制数据
//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, , 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, , 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;
}
【C#基础】HTTP发送POST二进制数据的更多相关文章
- Asp.net Core中SignalR Core预览版的一些新特性前瞻,附源码(消息订阅与发送二进制数据)
目录 SignalR系列目录(注意,是ASP.NET的目录.不是Core的) 前言 一晃一个月又过去了,上个月有个比较大的项目要验收上线.所以忙的脚不沾地.现在终于可以忙里偷闲,写一篇关于Signal ...
- Java模拟POST请求发送二进制数据
在进行程序之间数据通信时我们有时候就需要自定义二进制格式,然后通过HTTP进行二进制数据交互.交互的示例代码如下: public static void main(String[] args) { S ...
- [ActionScript 3.0] AS利用ByteArray向PHP发送二进制数据生成图片
flash as3向php发送二进制数据,通过php保存成图片. AS端: package { import com.JPEGEncoder.JPGEncoder; import flash.disp ...
- netty实现websocket发送文本和二进制数据
原文:https://huan1993.iteye.com/blog/2433552 最近在学习netty相关的知识,看到netty可以实现 websoket,因此记录一下在netty中实现webso ...
- [ActionScript 3.0] AS向php发送二进制数据方法之——在URLRequest中构造HTTP协议发送数据
主类 HTTPSendPHP.as package { import com.JPEGEncoder.JPGEncoder; import com.fylib.httpRequest.HttpRequ ...
- zeroMQ消息传送的是一个固定长度的二进制数据,而TCP是通过字节流,那二进制流和字节流有啥区别?
1.TCP是面向连接的,它在UDP数据包的基础上增加了重传.流控等一系列机制,封装成基于字节流的传输层通讯协议.使用字节流,应用有点像语音或视频聊天,粒度小到一个字节,双向同时传输,连绵不断. Zer ...
- 详细解读XMLHttpRequest(二)响应属性、二进制数据、监测上传下载进度
本文主要参考:MDN 分析并操作 responseXML属性 如果你使用 XMLHttpRequest 来获得一个远程的 XML 文档的内容,responseXML 属性将会是一个由 XML 文档解析 ...
- 巧用AWK处理二进制数据文件
AWK是Unix下的一款功能强大的文本格式化和抽取工具.利用这个工具,可以对复杂的文本文件进行整理,提取其中的全部或者部分数据,按照需要的格式予以显示.需要说明的是,AWK的强大功能只针对纯文本文件. ...
- Linux企业级项目实践之网络爬虫(16)——使用base64传输二进制数据
用http传输二进制的数据时,需要将二进制做一下转化,例如传输的int类型,将int类型之间转为char以后,丢失掉了长度的信息,如数字123456,本来只有4个字节,但是转化成文本的"12 ...
随机推荐
- memcache和数据库的使用技巧
1.加速无数据的访问速度毋庸置疑取数据先去取下memcache里的数据,如果没有再去数据库取数据但这样如果我取100次都是没有的那么我得去数据库去取100次 如果还是重复的...那么效率就不高了 解决 ...
- 第一次启动MySQL时报错
[root@localhost~]#/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mys ...
- Revenge of Fibonacc
算法:搜索: In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence rela ...
- JAVA-POI实现EXCEL的读写
想要完成JAVA读写EXCEL,首先需要JAVA-POI包的支持,百度搜索即可找到资源,不再赘述: POI-新增EXCEL并输入内容 package com.gsh.test.poi; import ...
- 2.2 文件 I/O 的基石:Path
Path通常代表文件系统中的位置,能浏览任何类型的文件系统,包括zip归档文件系统: 文件系统中的几个概念:目录树.根目录.绝对路径.相对路径: NIO.2中的Path是一个抽象构造,你所创建和处理的 ...
- 制作EDM 邮件规范
邮件模板最主要是保证兼容性,很多邮箱的过滤规则不同,因此邮件页面要使用最简单原始的代码实现内容展现. 一,采用table嵌套布局,避免用div布局,因为DIV布局会用到float等浮动样式,一些邮箱会 ...
- 解决.VS2012+EF5.0开发的网站在window server2003上无法部署的问题(转载)
转载:http://www.cnblogs.com/eggTwo/p/3653825.html (一)前 言 ...
- MOOTOOLS简单操作应用知识
在项目中我们经常需要用到全选/反选.等操作按钮. 基于mootools框架与jquery框架不一致.导致缓慢. $('chkall').addEvent('click',function(){ if( ...
- mysql 分区信息查看
select partition_name part,partition_expression expr,partition_description descr,table_rows from INF ...
- ServerInfo.INI解密
[GlobalInfo]LastServerName=000781ED2D127FBA074D97444DC82F216443034E66BB341A428B14E326A656B9LastServe ...