using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
HttpWebRequestTest();
HttpClientHandlerTest().Wait();
Console.ReadKey();
}
static void HttpWebRequestTest()
{
WebProxy proxy = new WebProxy("http://localhost:8001/");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.baidu.com");
request.Proxy = proxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string content = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
Console.WriteLine(content);
response.Close();
}
static async Task HttpClientHandlerTest()
{
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = new WebProxy("http://localhost:8001/"),
UseProxy = true
};
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync("https://www.baidu.com");
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)