1.请求方法
/// <summary> /// httpClient 请求接口 /// </summary> /// <param name="url">地址</param> /// <param name="xmlString">提交数据 json格式</param> /// <returns></returns> private string PostResponse(string url, string xmlString) { HttpContent httpContent = new StringContent(xmlString); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpClient httpClient = new HttpClient(); HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; if (response.IsSuccessStatusCode) { Task<string> t = response.Content.ReadAsStringAsync(); return t.Result; } return string.Empty; }
2.Api // POST api/values public HttpResponseMessage Post([FromBody]string value) { return Request.CreateResponse(HttpStatusCode.OK, value); }