C#通过HttpClient中Get Post获取数据


/// <summary>

   /// C# 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");
       HttpClient httpClient = new HttpClient();
       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)
   {
       HttpClient httpClient = new HttpClient();
       HttpResponseMessage res = httpClient.GetAsync(url).Result;
       if (res.IsSuccessStatusCode)
       {
           Task<string> t = res.Content.ReadAsStringAsync();
           return t.Result;
       }
       return string.Empty;
   }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM