C# HttpClient应用 下载302跳转文件,获取网页html代码,获取http api接口数据

180it 2024-11-19 AM 85℃ 0条
 /// <summary>
 /// 下载  302跳转文件
 /// </summary>
 /// <param name="parentUrl">父Url</param>
 /// <param name="url">302跳转的Url</param>
 /// <returns></returns>
 public bool DownFile(string parentUrl,string url)
 {
     using (HttpClient client = new HttpClient())
     {
         Uri uri = new Uri(parentUrl);
         client.DefaultRequestHeaders.Referrer = uri;
         var responseMessage = client.GetAsync(url).Result;
 
         if (responseMessage.IsSuccessStatusCode)
         {
             //var v = responseMessage.Content.Headers.ContentType;//内容类型
             using (var fs = File.Create("d:\\1.pdf"))
             {
                 // 获取结果,并转成 stream 保存到本地。
                 var streamFromService = responseMessage.Content.ReadAsStreamAsync().Result;
                 streamFromService.CopyTo(fs);
                 responseMessage.Dispose();
                 streamFromService.Dispose();
                 return true;
             }
         }
     }
     return false;
 }
 /// <summary>
 /// 下载 文件
 /// </summary>
 /// <param name="parentUrl">父Url</param>
 /// <param name="url">302跟转的Url</param>
 /// <returns></returns>
 public bool DownFile(string url)
 {
     using (HttpClient client = new HttpClient())
     {
         var responseMessage = client.GetAsync(url).Result;
         if (responseMessage.IsSuccessStatusCode)
         {
             //var v = responseMessage.Content.Headers.ContentType;//内容类型
             using (var fs = File.Create("d:\\1.pdf"))
             {
                 // 获取结果,并转成 stream 保存到本地。
                 var streamFromService = responseMessage.Content.ReadAsStreamAsync().Result;
                 streamFromService.CopyTo(fs);
                 responseMessage.Dispose();
                 streamFromService.Dispose();
                 return true;
             }
         }
     }
     return false;
 }
 public string Get(string urlStr)
 {
     using (HttpClient client = new HttpClient())
     {
         var responseMessage = client.GetAsync(urlStr).Result;
         if (responseMessage.IsSuccessStatusCode)
         {
             string data = responseMessage.Content.ReadAsStringAsync().Result;
             return data;
         }
         else
         {
             return string.Empty;
         }
     }
 }
 public string Post(string urlStr,string data)
 {
     using (HttpClient client = new HttpClient())
     {
         HttpContent content = new StringContent(data);
         content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
         string result = client.PostAsync(urlStr, content).Result.Content.ReadAsStringAsync().Result;
         return result;
     }
 }
 /// <summary>
 /// httpclient中携带cookie 
 /// </summary>
 /// <param name="url"></param>
 /// <param name="cookie">cookie字符串</param>
 /// <returns></returns>
 public string Get(string url, string cookie)
 {
     HttpClientHandler handler = new HttpClientHandler() { UseCookies = false };
     HttpClient client = new HttpClient(handler);
     HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, url);
     message.Headers.Add("Cookie", cookie);
     HttpResponseMessage result = client.SendAsync(message).Result;
     if (result.IsSuccessStatusCode)
     {
         return result.Content.ReadAsStringAsync().Result;
     }
     return "";
 }
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

C# HttpClient应用 下载302跳转文件,获取网页html代码,获取http api接口数据