Example #1
0
        public static Stream GetResponseImage(string pUrl, string pReferer = "", string pMethod = "GET", string pData = "", int pTime = 0x1770, string pEncoding = "UTF-8", bool pIsPTHander = true)
        {
            Stream result;

            try
            {
                HttpHelper.DeleteUrlCacheEntry(pUrl);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(pUrl);
                httpWebRequest.KeepAlive         = true;
                httpWebRequest.Method            = pMethod;
                httpWebRequest.AllowAutoRedirect = true;
                httpWebRequest.CookieContainer   = HttpHelper.CookieContainers;
                httpWebRequest.Referer           = pReferer;
                httpWebRequest.UserAgent         = HttpHelper.IE8;
                httpWebRequest.Accept            = "*/*";
                httpWebRequest.Timeout           = pTime;
                httpWebRequest.CachePolicy       = HttpHelper.rcp;
                httpWebRequest.Headers.Add("x-requested-with", "XMLHttpRequest");
                if (pIsPTHander)
                {
                    HttpHelper.PTRequest(ref httpWebRequest);
                }
                Encoding encoding = Encoding.GetEncoding(pEncoding);
                if (pMethod.ToUpper() == "POST" && pData != null)
                {
                    byte[] bytes = encoding.GetBytes(pData);
                    httpWebRequest.ContentLength = (long)bytes.Length;
                    httpWebRequest.ContentType   = "application/x-www-form-urlencoded; charset=UTF-8";
                    Stream requestStream = httpWebRequest.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                }
                ServicePointManager.ServerCertificateValidationCallback = (RemoteCertificateValidationCallback)Delegate.Combine(ServicePointManager.ServerCertificateValidationCallback, new RemoteCertificateValidationCallback((object se, X509Certificate cert, X509Chain chain, SslPolicyErrors sslerror) => true));
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                result = httpWebResponse.GetResponseStream();
            }
            catch
            {
                result = null;
            }
            return(result);
        }