• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【C#基础】HTTP发送POST二进制数据

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//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;

        }

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#深入浅出获取时间DateTime发布时间:2022-07-10
下一篇:
C#获取屏幕分辨率发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap