HttpHelper, httphelper class

Source: Internet
Author: User

HttpHelper, httphelper class

Public class HttpHelper {private static CookieContainer _ cc = new CookieContainer (); private static WebProxy _ proxy; private static int _ delayTime; private static int _ timeout = 120000; // The default is 120000 milliseconds (120 seconds ). private static int _ tryTimes = 3; // default retry 3 times private static string _ lastUrl = string. empty; private static string reqUserAgent = "Mozilla/4.0 (compatible; MSIE 7. 0; Windows NT 6.0; Server Load balancer ;. net clr 2.0.50727 ;. net clr 3.0.04506; InfoPath.2) "; // Get public static string Get (string url) {string data = Get (url, _ lastUrl) without referer; _ lastUrl = url; return data ;} /// <summary> /// Get data from server /// </summary> /// <param name = "url"> </param> /// <returns> Return content </returns> public static string Get (string url, string referer) {int failedTimes = _ TryTimes; while (failedTimes --> 0) {try {DelaySomeTime (); HttpWebRequest req = (HttpWebRequest) WebRequest. create (new Uri (url); req. userAgent = reqUserAgent; req. cookieContainer = _ cc; req. referer = referer; req. method = "GET"; req. timeout = _ timeout; if (null! = _ Proxy & null! = _ Proxy. credentials) {req. useDefaultCredentials = true;} req. proxy = _ proxy; // receives the returned string HttpWebResponse res = (HttpWebResponse) req. getResponse (); StreamReader sr = new StreamReader (res. getResponseStream (), Encoding. UTF8); return sr. readToEnd ();} catch (Exception e) {// TraceLog. error ("http get Error:" + e. message); // TraceLog. error ("Url:" + url) ;}} return string. empty;} public static void Do Wnload (string url, string localfile) {WebClient client = new WebClient (); client. downloadFile (url, localfile);} private static void DelaySomeTime () {if (_ delayTime> 0) {Random rd = new Random (); int delayTime = _ delayTime * 1000 + rd. next (1, 1000); Thread. sleep (delayTime );}} /// <summary> /// Set Proxy /// </summary> /// <param name = "server"> </param> /// <param name = "port"> </param> public stat Ic void SetProxy (string server, int port, string username, string password) {if (null! = Server & port> 0) {_ proxy = new WebProxy (server, port); if (null! = Username & null! = Password) {_ proxy. credentials = new NetworkCredential (username, password); _ proxy. bypassProxyOnLocal = true ;}}} /// <summary> /// Set delay connect time /// </summary> /// <param name = "delayTime"> </param> public static void SetDelayConnect (int delayTime) {_ delayTime = delayTime ;} /// <summary> /// Set the timeout for each http request // </summary> /// <param name = "timeout"> </param> public static void SetTimeOut (int timeout) {if (timeout> 0) {_ timeout = timeout ;}} /// <summary> // Set the try times for each http request // </summary> /// <param name = "times"> </param> public static void SetTryTimes (int times) {if (times> 0) {_ tryTimes = times ;}}}

 


What does HttpHelper in C # Mean?

C # does not contain HttpHelper. It is a class written by someone else called HttpHelper.

What references and namespaces need to be added to httphelper?

System. Net;
System. IO;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.