帮助中心

   

热门搜索: 免费续期  推广  永久会员

代理IP知识:屏蔽代理服务器代码分享

网络安全这个议题是我们在进行互联网“冲浪”时不能够忽视的问题,因为根据相关数据统计,现在的网络攻击事件发生次数是越来越多了,而且攻击者的技术也越发高明,导致有时定位攻击者成为了困难的事情。那么,下面代理IP为大家分享屏蔽代理服务器的代理,代码详情如下。

 

 

写个网页request类:

 

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Net;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

namespace ConsoleApplication1

{

public class WebRequestUtil

{

public static string responseBody = string.Empty;

public static bool GetWeb(string uri, string proxyAddress = “”, int proxyPort = 0)

{

string serverUri = string.Format(uri);

////set limit for supporting 200 connection

ServicePointManager.DefaultConnectionLimit = 1000;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUri);

if (!string.IsNullOrEmpty(proxyAddress))

{

WebProxy myproxy = new WebProxy(proxyAddress, proxyPort);

request.Proxy = myproxy;

}

////extend timeout for decrease request timeout re-trying times

request.Timeout = 60 * 1000;

request.Method = @”GET”;

UTF8Encoding encoding = new UTF8Encoding();

request.Headers.Set(“Cache-Control”, @”no-cache”);

request.UserAgent = “Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)”;

try

{

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

bool isSent = false;

int retryCount = 0;

string errorStr = string.Empty;

while (!isSent && retryCount <= 2)

{

retryCount++;

try

{

using (StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))

{

responseBody = stream.ReadToEnd();

}

isSent = true;

}

catch (Exception exc)

{

if (!errorStr.Contains(exc.ToString()))

{

errorStr += exc.ToString();

}

////Re-try when operation timeout

if (!exc.ToString().Contains(“The operation has timed out”))

{

LogError(exc.ToString());

}

Thread.Sleep(1000);

}

}

if (retryCount > 100)

{

string err = string.Format(“request.GetRequestStream try 100 times and timeout! detail error: {0}”, errorStr);

LogError(err);

return false;

}

////need to close or abort request for each call to fix timeout issue, otherwise it will fail when the 3rd call!

if (request != null)

{

request.Abort();

}

if (response.StatusCode != HttpStatusCode.OK)

{

string err = string.Format(“Failed, error:{1}”, response.ToString());

LogError(err);

return false;

}

if (response != null)

{

response.Close();

}

}

catch (Exception exc)

{

LogError(exc.ToString());

return false;

}

return true;

}

public static void LogError(string content)

{

File.AppendAllText(“log.log”, “ERROR: ” + content + Environment.NewLine);

}

}

}

 

迅联加速,专业的动态IP服务商,拥有全国180+个城市的线路,3000万+可用IP库,7*24小时不断供应IP。

关闭
19907977089
关注微信公众号,免费送30分钟时长