8-30 1,229 views
HttpClient 设置代理
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = new WebProxy("45.63.58.40:8888"),
UseProxy = true
};
HttpClient httpClient = new HttpClient(handler);
WebRequest 设置代理
WebRequest myWebRequest = WebRequest.Create(url);
myWebRequest.Method = "GET";
myWebRequest.Proxy = new WebProxy("45.63.58.40:8888");
完整示例
public static void proxy_HttpClient()
{
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = new WebProxy("http://127.0.0.1:1080"),
UseProxy = true,
};
HttpClient httpClient = new HttpClient(handler);
String url = "http://typhoon.fly3w.com";
HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result;
String result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
Console.ReadLine();
}
public static void proxy_WebRequest()
{
// Create a new request to the mentioned URL.
WebRequest myWebRequest = WebRequest.Create("https://typhoon.fly3w.com/");
myWebRequest.Method = "DELETE";
//WebProxy myProxy = new WebProxy();
// Obtain the Proxy Prperty of the Default browser.
//myProxy = (WebProxy)myWebRequest.Proxy;
myWebRequest.Proxy = new WebProxy("112.74.26.117:80");
//WebResponse myWebResponse = myWebRequest.GetResponse();
WebResponse myWebResponse = myWebRequest.GetResponse();
// Print the HTML contents of the page to the console.
Stream streamResponse = myWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
Char[] readBuff = new Char[256];
int count = streamRead.Read(readBuff, 0, 256);
Console.WriteLine("\nThe contents of the Html pages are :");
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
Console.Write(outputData);
count = streamRead.Read(readBuff, 0, 256);
}
// Close the Stream object.
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse Resource.
myWebResponse.Close();
Console.WriteLine("\nPress any key to continue.........");
Console.Read();
}