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();
                }
    
            }
  • 相关阅读:
    USACO / Longest Prefix最长前缀(DP)
    (转)HDOJ 4006 The kth great number(优先队列)
    STL priority_queue(优先队列(堆))
    康托展开
    USACO / Magic Squares(经典BFS+Cantor展开hash)
    国家集训队论文分类整理
    国家集训队论文分类整理
    国家集训队论文分类整理
    OI / ACM 知识归纳
    学年总结跋涉ACM之路
  • 原文地址:https://www.cnblogs.com/njcxwz/p/5864404.html
Copyright © 2011-2022 走看看