zoukankan      html  css  js  c++  java
  • 使用WebHelper调用Asp.net WebAPI

    1.WebHelper

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Collections.Specialized;
     4 using System.Linq;
     5 using System.Net;
     6 using System.Net.Http;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 
    10 namespace CommonFoundation.Common
    11 {
    12     public class WebHelper
    13     {
    14         public WebHelper(string _url)
    15         {
    16             url = _url;
    17         }
    18         private string url;
    19         /// <summary>
    20         /// 接口调用地址
    21         /// </summary>
    22         public string Url
    23         {
    24             get { return url; }
    25             set { url = value; }
    26         }
    27         /// <summary>
    28         /// get请求
    29         /// </summary>
    30         /// <returns></returns>
    31         public async Task<string> get()
    32         {
    33             var result = string.Empty;
    34             using (var client = new HttpClient())
    35             {
    36                 result = await client.GetStringAsync(url);
    37             }
    38             return result;
    39         }
    40 
    41         /// <summary>
    42         /// post请求
    43         /// </summary>
    44         /// <param name="value">参数值</param>
    45         /// <returns></returns>
    46         public async Task<string> post(string value)
    47         {
    48             //var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
    49             var result = string.Empty;
    50             using (var client = new HttpClient())
    51             {
    52                 var values = new List<KeyValuePair<string, string>>();
    53                 values.Add(new KeyValuePair<string, string>("", value));
    54                 var content = new FormUrlEncodedContent(values);
    55                 var response = await client.PostAsync(url, content);
    56                 var responseString = await response.Content.ReadAsStringAsync();
    57                 result = responseString;
    58             }
    59             return result;
    60         }
    61 
    62         public string post1()
    63         {
    64             string result = "";
    65             using (var client = new WebClient())
    66             {
    67                 var values = new NameValueCollection();
    68                 values[""] = "shunfeng";
    69 
    70                 var response = client.UploadValues("http://localhost:15954/api/test", values);
    71 
    72                 var responseString = Encoding.Default.GetString(response);
    73             }
    74             return result;
    75         }
    76     }
    77 }

    2.调用

     1 private static async void test()
     2         {
     3             var url = "https://www.bjcytxkj.com/api/express";
     4             WebHelper webHelper = new WebHelper(url);
     5             var dic = new Dictionary<string, string>();
     6             dic["ShipperName"] = "SF";
     7             dic["ShipperCode"] = "502947304577";
     8             string str = JsonConvert.SerializeObject(dic);
     9             var result1 = await webHelper.get();
    10             var result = await webHelper.post(str);
    11             result = result.Replace("\", "");
    12             Console.WriteLine(result);
    13         }
  • 相关阅读:
    King's Quest
    Prince and Princess
    Strongly connected
    线性渐变--linear-gradient
    镜像渐变-radio-gradient
    宽度自适应-左右
    宽度自适应-左中右
    HTML5 视频规范简介
    SVG格式
    Html5新标签解释及用法
  • 原文地址:https://www.cnblogs.com/cnki/p/6053104.html
Copyright © 2011-2022 走看看