【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 ...
随机推荐
- C#正则怎么判断字符串中是否有汉字
Regex r = new Regex(".*[\\u4e00-\\u9faf].*");r.IsMatch(username)
- ReSharper warning: Virtual member call in a constructor
1.构造函数的执行顺序是:基类--->派生类 2.如果虚方法被重写后,由于基类中调用了虚方法,此时调用的是最外层的被重写后的虚方法,此时可能会发生异常 举例: class Parent { pu ...
- Asp服务器控件(HyperLink、Button) 绑定后台参数 DataBinder.Eval
HyperLink动态绑定参数 <asp:HyperLink id="MbCenterHLnk" runat="server" Text='会员中心' T ...
- 解决Access restriction问题
今天在导入一个包时报错了
- QT5控件-QPushButton和QFocusFrame(按钮和焦点框)
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> ...
- [每日一题] OCP1z0-047 :2013-07-24 子查询――外查询与内查询的执行顺序
一.Oracle的子查询分为两类分别是嵌套子查询和非嵌套子查询.所谓嵌套子查询是指,子查询是一个独立的查询不与外部查询相关,子查询将被先执行,而且只被执行一次,子查询执行完成后,再执行外部的查询,外部 ...
- DoingOrder.aspx.cs缓存的使用方法
using System; using System.Web.UI; using System.Data; using System.Text; using BLL = SmartWaterSys.B ...
- 在线支付接口之PHP支付宝接口开发简单介绍
php100:92:在线支付接口之PHP支付宝接口开发 支付接口一般是第三方提供的代收款.付款的平台,可以通过支付接口帮助企业或个人利用一切可以使用的支付方式.常见支付平台:支付宝.快钱.云网支付.财 ...
- Css透明度
全透明代码:{background:transparent} 半透明代码:{filter:alpha(opacity=80);-moz-opacity:0.8;width:auto !importan ...
- 微软SpeechRecognitionEngine
API官网手册:http://msdn.microsoft.com/zh-cn/library/System.Speech.Recognition.SpeechRecognitionEngine(v= ...