zoukankan      html  css  js  c++  java
  • HttpWebRequest调用WebAPI

     private void button1_Click(object sender, EventArgs e)
            {
               string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:"test089",Name:"test1"}");
    
            }
    
            public static string HttpPost(string url, string body)
            {
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json";
           
                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
    
            }
      private void button1_Click(object sender, EventArgs e)
            {
                string ss = HttpGet("http://localhost:41558/api/Demo/GetXXX?Name=北京");
    
            }
    
            public static string HttpGet(string url)
            {
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json";
               
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
    
            }
  • 相关阅读:
    读书笔记—CLR via C#线程25-26章节
    算法回顾--N皇后问题简单回顾
    编程拾趣--集合子集问题
    读书笔记—CLR via C#异常和状态管理
    读书笔记—CLR via C#字符串及文本
    设计模式---抽象工厂
    读书笔记—CLR via C#反射
    读书笔记—CLR via C#委托和attribute
    C#编程实践—EventBroker简单实现
    Linux平台屏幕录像工具RecordMyDesktop
  • 原文地址:https://www.cnblogs.com/njcxwz/p/5864404.html
Copyright © 2011-2022 走看看