• http Get 发送body数据


    public static string HttpGet(string Url, string header = "", string senddata = "")
            {
                //byte[] dataArray = System.Text.Encoding.UTF8.GetBytes(senddata);
                // Console.Write(Encoding.UTF8.GetString(dataArray));
    
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url);
                request.Method = "get";
                request.ContentType = "application/json";
                if (!string.IsNullOrEmpty(header))
                {
                    request.Headers.Add("Authorization", header);
                }
                
                //request.ContentLength = dataArray.Length;
                //request.CookieContainer = cookie;
                if (!string.IsNullOrEmpty(senddata))
                {
                    var type = request.GetType();
                    var currentMethod = type.GetProperty("CurrentMethod", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(request,null);
                    var methodType = currentMethod.GetType();
                    methodType.GetField("ContentBodyNotAllowed", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentMethod, false);
    
                    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                    {
                        streamWriter.Write(senddata);
                    }
                }
                
                try
                {
                    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                    System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                    String res = reader.ReadToEnd();
                    reader.Close();
                    return res.ToString();
                }
                catch (Exception e)
                {
                    return e.Message + e.ToString();
                }
            }
    

      

  • 相关阅读:
    安全体系(一)—— DES算法详解
    Windows下将gvim8配置为Python IDE
    SSH原理记录
    Spring Cloud 注册中心Eureka
    Spring Cloud 声明式服务调用 Feign
    Spring Cloud 客服端负载均衡 Ribbon
    Http服务器实现文件上传与下载(四)
    1.项目优化
    分支的创建与代码的推送——提交用户列表功能代码
    主页Header布局
  • 原文地址:https://www.cnblogs.com/v587yy/p/14150727.html
Copyright © 2020-2023  润新知