c# 使用httpclient

时间:2023-03-09 09:27:39
c# 使用httpclient
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("开始访问网络");
GetData();
Console.WriteLine("结束访问");
Console.ReadKey();
}
static async void GetData()
{
HttpClientHandler hander = new HttpClientHandler();
hander.AllowAutoRedirect = false;
HttpClient httpClient = new HttpClient(hander); HttpResponseMessage response = null;
// string NavigateUrl = "http://おまとめローン審査基準.com/%E3%82%AA%E3%82%B9%E3%82%B9%E3%83%A1%E3%81%AE%E3%81%8A%E3%81%BE%E3%81%A8%E3%82%81%E3%83%AD%E3%83%BC%E3%83%B3.php";
string NavigateUrl = "http://www.baidu.com";
Uri uri = new Uri(NavigateUrl);
IdnMapping idn = new IdnMapping();
NavigateUrl = NavigateUrl.Replace(uri.Host, idn.GetAscii(uri.Host));
response = await httpClient.GetAsync(NavigateUrl);
// string loction = httpClient.GetAsync(NavigateUrl).Result.Headers.Location.ToString();
if (response.IsSuccessStatusCode)
{
string responseBodyAsText = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(responseBodyAsText);
}
}
}
}