• C# 调用 WebApi


    一.WebRequest方式

    Post:

    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();
        }
    }
      
    // 调用
    string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:"test089",Name:"test1"}");

    Get:

    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();
        }
    }
    // 调用
    string ss = HttpGet("http://localhost:41558/api/Demo/GetXXX?Name=北京");

    二.HttpClient 方式

    Post:

         //提交数据操作
         //创建一个处理序列化的DataContractJsonSerializer
         DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));
         MemoryStream ms = new MemoryStream();
         //将资料写入MemoryStream
         serializer.WriteObject(ms, new People() { Id = 1, Name = "Hello ni" });
         //一定要在这设定Position
         ms.Position = 0;
         HttpContent content = new StreamContent(ms);//将MemoryStream转成HttpContent
         content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
         HttpClient client = new HttpClient();
         //由HttpClient发出Put Method
         HttpResponseMessage response = await client.PutAsync("http://localhost:41558/api/Demo"+ "/1", content);
         if (response.IsSuccessStatusCode)
             MessageBox.Show("成功");
             
             
         //删除操作         
         HttpClient client = new HttpClient();
         //由HttpClient发出Delete Method
         HttpResponseMessage response = await client.DeleteAsync("http://localhost:41558/api/Demo"+"/1");
         if (response.IsSuccessStatusCode)
             MessageBox.Show("成功"); 

    Get:

    using (WebClient client = new WebClient())
    {
         client.Headers["Type"] = "GET";
         client.Headers["Accept"] = "application/json";
         client.Encoding = Encoding.UTF8;
         client.DownloadStringCompleted += (senderobj, es) =>
         {
             var obj = es.Result;
         };
         client.DownloadStringAsync("http://localhost:41558/api/Demo");
    }
    *****************************************************
    *** No matter how far you go, looking back is also necessary. ***
    *****************************************************
  • 相关阅读:
    由mysql数据库基础上的php程序实现单词的查询、删除、更改和查询
    hdu 4756 Install Air Conditioning
    textarea中限制输入字符长度(实用版)
    ACCESS数据库操作教程
    ASP + ACCESS 上传图片到数据库与将图片读出数据库显示之实现(详细版)
    ASP + ACCESS 上传图片到数据库与将图片读出数据库显示之实现
    ASP + ACCESS保存图片文件之实现
    VB将PDF流写入ACCESS数据库,通过AcroPDF控件读出PDF流之实现
    管理员登陆界面与数据库操作界面设计
    textarea中限制输入字符长度
  • 原文地址:https://www.cnblogs.com/gangle/p/9229091.html
Copyright © 2020-2023  润新知