public static String HttpGet(Uri uri, System.Net.Cache.RequestCacheLevel cachePolicy)
 {
     WebRequest wreq = WebRequest.Create(uri);
     wreq.CachePolicy = new System.Net.Cache.RequestCachePolicy(cachePolicy);
     using (var wresp = wreq.GetResponse())
     using (var wrespStream = wresp.GetResponseStream())
     using (var reader = new System.IO.StreamReader(wrespStream))
         return reader.ReadToEnd();
 }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the HttpUtil class.
        /// </summary>
        /// <param name="url">The url of the request to make</param>
        /// <param name="cachesettings">The cache settings (important note: this is always NoCacheNoStore under Mono)</param>
        /// <param name="postdata">POST data for a POST Http request.</param>
        /// <returns></returns>
        public HttpUtil(string url, System.Net.Cache.RequestCacheLevel cachesettings, string postdata)
        {
            String protocolhandler = "";

            if (!url.Contains("://"))
            {
                protocolhandler = "https:";
                if (!Settings.ProgramHttpsLinks)
                {
                    protocolhandler = "http:";
                }
            }

            url = protocolhandler + url;
            if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                this.url = url;
            }
            else
            {
                Log.Write(LogType.exception, "Invalid url.");
            }

            if (!String.IsNullOrEmpty(postdata))
            {
                this.postdata = postdata;
            }
            else
            {
                this.postdata = null;
            }

            this.cachesettings      = cachesettings;
            this.httpthread         = new BackgroundWorker();
            this.httpthread.DoWork += new DoWorkEventHandler(this.httpthread_DoWork);
        }
示例#3
0
 public RequestCachePolicy(System.Net.Cache.RequestCacheLevel level)
 {
 }
示例#4
0
 /// <summary>
 /// Initializes a new instance of the HttpUtil class. Using no http POST.
 /// </summary>
 /// <param name="url"></param>
 /// <param name="cachesettings"></param>
 public HttpUtil(string url, System.Net.Cache.RequestCacheLevel cachesettings) : this(url, cachesettings, null)
 {
 }
示例#5
0
        /// <summary>
        /// Create a WebRequest object
        /// </summary>
        /// <param name="url">Http request url.</param>
        /// <param name="cachesettings">Http cache settings</param>
        /// <returns>A new httpwebrequest object.</returns>
        private HttpWebRequest CreateHttpWebRequest(string url, System.Net.Cache.RequestCacheLevel cachesettings)
        {
            HttpWebRequest request = null;

            if (String.IsNullOrEmpty(url))
            {
                Log.Write(LogType.exception, "Url is null or empty.");
                return(request);
            }

            System.Net.ServicePointManager.Expect100Continue      = false;
            System.Net.ServicePointManager.EnableDnsRoundRobin    = true;
            System.Net.ServicePointManager.DnsRefreshTimeout      = 3 * 60 * 1000; // 180000 ms = 180 s = 3 minutes
            System.Net.ServicePointManager.DefaultConnectionLimit = 8;

            // .NET framework 3.5 and lower do not have TLS 1.1 and TLS 1.2 support only vulnerable TLS 1.0 and SSL 3.0.
            // .NET framework 4.0 has TLS 1.1 and TLS 1.2 support but has no enum available for it.
            // SSL 3.0 and TLS 1.0 are vulnerable and should be avoided.
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
            System.Net.ServicePointManager.CheckCertificateRevocationList = true;
            Log.Write(LogType.info, "Making request to '" + url + "'");
            try
            {
                request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                if (this.postdata == null)
                {
                    request.Method = "GET";
                }
                else
                {
                    request.Method = "POST";
                }

                request.ContentType       = "text/xml";
                request.ProtocolVersion   = HttpVersion.Version11; // HTTP 1.1>= is required, required sending host: header for notefly.org
                request.UserAgent         = Program.AssemblyTitle + " " + Program.AssemblyVersionAsString;
                request.AllowAutoRedirect = false;
                request.Timeout           = Settings.NetworkConnectionTimeout;
                request.KeepAlive         = true;

                if (Settings.NetworkProxyEnabled && !string.IsNullOrEmpty(Settings.NetworkProxyAddress))
                {
                    request.Proxy = new WebProxy(Settings.NetworkProxyAddress, Settings.NetworkProxyPort);
                }
                else
                {
                    // set proxy to nothing, otherwise HttpWebRequest has issues, details: https://holyhoehle.wordpress.com/2010/01/12/webrequest-slow/
                    request.Proxy = null;
                }

                if (Settings.NetworkUseGzip)
                {
                    request.Headers["Accept-Encoding"] = "gzip";
                    request.AutomaticDecompression     = DecompressionMethods.GZip;
                }

                request.CachePolicy         = new System.Net.Cache.RequestCachePolicy(cachesettings);
                request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
                request.PreAuthenticate     = false;
                if (this.postdata != null)
                {
                    request.ContentType = "application/x-www-form-urlencoded";
                    byte[] dataenc = System.Text.Encoding.UTF8.GetBytes(this.postdata); // utf-8 on server.
                    request.ContentLength = dataenc.Length;
                    Stream stream = request.GetRequestStream();
                    stream.Write(dataenc, 0, dataenc.Length);
                }
            }
            catch (System.Net.WebException webexc)
            {
                Log.Write(LogType.exception, webexc.Message + webexc.StackTrace);
            }

            return(request);
        }
示例#6
0
 public static String HttpGet(string uri, System.Net.Cache.RequestCacheLevel cachePolicy)
 {
     return(HttpGet(new Uri(uri), cachePolicy));
 }