客户端发送get请求

前言:

  订餐系统需要做一个读卡器的辅助程序,将读到的卡号通过swagger发送到服务器中,这就涉及到了C/S端程序发送get请求,因此本人在网上去查找了相关的知识和博客,最终有了下边的解决办法。

简单发送get请求

        /// <summary>
        /// 客户端发送get请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ApiGet(string url)
        {
            HttpClient client = new HttpClient();
            //下边的uri是请求的网址
            client.BaseAddress = new Uri("http://XXX.XXX.XXX:XXX");
            // 为JSON格式添加一个Accept报头
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = client.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                return response.Content.ReadAsStringAsync().Result;
            }
            return "";
        }

接着我们再调用ApiGet这个方法就可以正常的发送get请求了,非常的简单,希望对大家有帮助!

猜你喜欢

转载自blog.csdn.net/wjf1997/article/details/91357771