通知:由于公司业务变更,动态住宅代理业务与2023年10月正式下线!网站目前只售卖静态住宅代理和数据中心代理,所有有关动态住宅代理的功能将不再正常使用。代码已失效
Using WebRequest
using System;
using System.IO;
using System.Net;
namespace ProxyRequest
{
class MainClass
{
public static void Main(string[] args)
{
// 更改代理登录、密码、IP和端口为您的代理列表中的值(如果使用用户名/密码进行授权)。
var myProxy = new WebProxy("http://Host:Port", true);
myProxy.Credentials = new NetworkCredential("login", "password");
var request = (HttpWebRequest)WebRequest.Create("http://ip-api.com");
request.Proxy = myProxy;
request.PreAuthenticate = true;
request.AllowAutoRedirect = false;
request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
var response = request.GetResponse();
Console.WriteLine("响应状态: " + ((HttpWebResponse)response).StatusDescription);
Console.WriteLine("\n响应头部:\n" + ((HttpWebResponse)response).Headers);
var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine("响应正文:\n" + responseFromServer);
reader.Close();
response.Close();
}
}
}
request.AllowAutoRedirect = false;
request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
var response = request.GetResponse();
Console.WriteLine("Response Status: " + ((HttpWebResponse)response).StatusDescription);
Console.WriteLine("\nResponse Headers:\n" + ((HttpWebResponse)response).Headers);
var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine("Response Body:\n" + responseFromServer);
reader.Close();
response.Close();
}
}
}
使用 HttpClient 进行异步请求 #
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace AsyncProxyRequest
{
class MainClass
{
static void Main(string[] args)
{
_ = AsyncMain();
Console.ReadKey();
}
public static async Task AsyncMain()
{
string requestUri = "http://ip-api.com"; // 请求的目标 URL
string crawleraApiKey = "login:password"; // API KEY
string crawleraAuth = Convert.ToBase64String(Encoding.Default.GetBytes(crawleraApiKey));
string crawleraUri = string.Format("{0}:{1}", "IP", "port"); // Crawlera 代理服务器的 IP 和端口
WebProxy proxy = new WebProxy(crawleraUri, true);
CookieContainer cookieContainer = new CookieContainer();
HttpClientHandler httpClientHandler = new HttpClientHandler()
{
Proxy = proxy,
PreAuthenticate = false,
UseDefaultCredentials = false,
UseProxy = true,
UseCookies = true,
CookieContainer = cookieContainer
};
HttpClient client = new HttpClient(httpClientHandler);
int _TimeoutSec = 300; // 请求超时时间(以秒为单位)
client.Timeout = new TimeSpan(0, 0, _TimeoutSec);
client.DefaultRequestHeaders.Add("Proxy-Authorization", "Basic " + crawleraAuth);
try
{
HttpResponseMessage response = await client.GetAsync(requestUri);
HttpContent content = response.Content;
Console.WriteLine("Response Status Code: " + (int)response.StatusCode); // 输出响应的状态码
string scrapedPage = await content.ReadAsStringAsync();
Console.WriteLine("Response Content: " + scrapedPage); // 输出响应的内容
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); // 输出异常信息
}
}
}
}