zoukankan      html  css  js  c++  java
  • 【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, 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;
    
            }

     

  • 相关阅读:
    将C#文档注释生成.chm帮助文档
    Html5shiv
    C#创建COM组件
    WebBrowser控件使用详解
    iframe跨域
    VMware Workstation 虚拟机暂停后无法启动 出现Exception 0xc0000006 (disk error while paging) has occurred.错误
    Java Timer 定时器的使用
    adf笔记
    JS编码解码
    【Python】Django CSRF问题
  • 原文地址:https://www.cnblogs.com/jhli/p/5911804.html
Copyright © 2011-2022 走看看