1. 程式人生 > >關於c#裏面的httpclient的調用

關於c#裏面的httpclient的調用

res cli eas es2017 accept src blog iss ica

使用http調用接口的辦法

//下載using System.Net.Http;

項目中的具體使用的方法

get

技術分享

post

HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            string url = "http://api.oa.com/api/Test/Post";
            var json = "{ \"Name\": \"Test\" }";
            var httpContent = new StringContent(json, Encoding.UTF8);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = client.PostAsJsonAsync(url, httpContent).Result;
            if (!response.IsSuccessStatusCode)
            {
                Response.Write(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase));
            }

關於c#裏面的httpclient的調用