How to integrate ip Agent in C # Sharp
This article mainly introduces how to integrate ip proxy knowledge in C#Sharp, the content is detailed and easy to understand, the operation is simple and fast, and has certain reference value. I believe everyone will gain something after reading this article on how to integrate ip proxy in C#Sharp. Let's take a look at it together.
I. Foreword
The company recently added a new project that required crawling aviation information data, and the other party had ip blocking. So I need to implement ip automatic switching in C Sharp to complete the crawling task.
second example
C Sharp Code Sample
//Target page to visit
string targetUrl = "http://httpbin.org/ip";
//proxy server
string proxyHost = "http://t.16yun.cn";
string proxyPort = "6447";
//proxy tunnel authentication information
string proxyUser = "16IXNGXO";
string proxyPass = "366338";
//Set proxy server
WebProxy proxy = new WebProxy(string.Format("{0}:{1}", proxyHost, proxyPort), true);
ServicePointManager.Expect100Continue = false;
var request = WebRequest.Create(targetUrl) as HttpWebRequest;
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.Method = "GET";
request.Proxy = proxy;
//request.Proxy.Credentials = CredentialCache.DefaultCredentials;
request.Proxy.Credentials = new System.Net.NetworkCredential(proxyUser, proxyPass);
//Set Proxy Tunnel
// Random ran=new Random();
// int tunnel =ran.Next(1,10000);
// request.Headers.Add("Proxy-Tunnel", String.valueOf(tunnel));
//request.Timeout = 20000;
//request.ServicePoint.ConnectionLimit = 512;
//request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36";
//request.Headers.Add("Cache-Control", "max-age=0");
//request.Headers.Add("DNT", "1");
//String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(proxyUser + ":" + proxyPass));
//request.Headers.Add("Proxy-Authorization", "Basic " + encoded);
using (var response = request.GetResponse() as HttpWebResponse)
using (var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string htmlStr = sr.ReadToEnd();
}
This is an example of the code, this demo can be copied directly, so I copied it directly, where the agent configuration is the information that you need to contact the customer service activation agent to obtain.
III. Precautions
The minimum IP support is 5 requests per second (if larger requests are needed, the corresponding product can be selected). So, when it defaults to 5 requests, I need to speed limit the crawler.
About "C#Sharp how to integrate ip proxy" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "how to integrate ip agents in C#Sharp" knowledge. If you still want to learn more knowledge, please pay attention to the industry information channel.