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;
    
            }

     

  • 相关阅读:
    Java中的Set List HashSet互转
    Java数组如何转为List集合
    Map
    Jave中的日期
    mybatis plus 条件构造器queryWrapper学习
    Error running 'JeecgSystemApplication': Command line is too long. Shorten command line for JeecgSystemApplication or also for Spring Boot default configuration.
    RBAC权限系统分析、设计与实现
    html拼接时onclick事件传递json对象
    PostgreSQL 大小写问题 一键修改表名、字段名为小写 阅读模式
    openssl创建的自签名证书,使用自签发证书--指定使用多域名、泛域名及直接使用IP地址
  • 原文地址:https://www.cnblogs.com/jhli/p/5911804.html
Copyright © 2011-2022 走看看