zoukankan      html  css  js  c++  java
  • HttpClient Get与Post请求数据

    直接干货:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Threading.Tasks;
    
    namespace FEG.ESB.Common.Helper
    {
        /// <summary>
        /// 
        /// </summary>
        public class HttpClientHelper
        {
            private static readonly HttpClient httpClient = new HttpClient();
    
    
            /// <summary>
            /// Post 请求
            /// </summary>
            /// <param name="url"></param>
            /// <param name="dic"></param>
            /// <returns></returns>
            public static string PostString(string url, Dictionary<string, string> dic)
            {
                string responseString = string.Empty;
                try
                {
                    var content = new FormUrlEncodedContent(dic);
                    var response = httpClient.PostAsync(url, content).Result;
                    responseString = response.Content.ReadAsStringAsync().Result;
                }
                catch (Exception)
                {
                }
                return responseString;
            }
    
            // <summary>
            /// post提交并反馈结果
            /// </summary>
            /// <param name="url"></param>
            /// <param name="xmlString"></param>
            /// <returns></returns>
            public static string PostXmlResponse(string url, string xmlString)
            {
                HttpContent httpContent = new StringContent(xmlString);
                httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/josn");
                HttpResponseMessage res = httpClient.PostAsync(url, httpContent).Result;
                if (res.IsSuccessStatusCode)
                {
                    Task<string> t = res.Content.ReadAsStringAsync();
                    return t.Result;
                }
                return string.Empty;
            }
            /// <summary>
            /// Get请求数据
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public static string GetResponse(string url)
            {
                HttpResponseMessage res = httpClient.GetAsync(url).Result;
                if (res.IsSuccessStatusCode)
                {
                    Task<string> t = res.Content.ReadAsStringAsync();
                    return t.Result;
                }
                return string.Empty;
            }
        }
    }

    Post 或者如下调用:

    
    
    //--HttpClientHelper.PostString(url,values);
          try
                {
                    System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
                    var values = new Dictionary<string, string>
                    {
                        { "cou_id", "A9E52C76-0CE7-4261-54C3-7EB04062F758" }
                    };
    
                    var content = new System.Net.Http.FormUrlEncodedContent(values);
                    var response = httpClient.PostAsync("http://localhost:8888/Api/Course/GetCourseStudent", content).Result;
                    var responseString = response.Content.ReadAsStringAsync().Result;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }

    
    
  • 相关阅读:
    Mybatis的XML中数字不为空的判断
    初步使用VUE
    Vue中实现菜单下拉、收起的动画效果
    Docker For Windows时间不对的问题
    freemarker使用自定义的模板加载器通过redis加载模板
    .net core 打印请求和响应的内容
    codedecision P1113 同颜色询问 题解 线段树动态开点
    洛谷P2486 [SDOI2011]染色 题解 树链剖分+线段树
    洛谷P3150 pb的游戏(1)题解 博弈论入门
    codedecision P1112 区间连续段 题解 线段树
  • 原文地址:https://www.cnblogs.com/Fengge518/p/12266911.html
Copyright © 2011-2022 走看看