Пример #1
0
        /// <summary>
        ///     Gets all type request data.
        /// </summary>
        /// <param name="parm"></param>
        /// <param name="postParamDict"></param>
        /// <returns></returns>
        public static string GetRequestResult(this ResponseParam parm, IDictionary <string, string> postParamDict = null)
        {
            if (parm is null)
            {
                throw new ArgumentNullException(nameof(parm));
            }

            var postData = string.Empty;

            if (postParamDict != null)
            {
                postData = CreateParameter(postParamDict);
            }

            var response = parm.CreateHttpResponse(postData);

            var stream = response.GetResponseStream();

            if (stream is null)
            {
                throw new NullReferenceException(nameof(stream));
            }

            using (var reader = new StreamReader(stream, parm.EncodeType))
                return(reader.ReadToEnd());
        }
Пример #2
0
        /// <summary>
        ///     Gets all type request model data.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parm"></param>
        /// <param name="postParamDict"></param>
        /// <returns></returns>
        public static T GetRequestResult <T>(this ResponseParam parm, IDictionary <string, string> postParamDict = null)
        {
            if (parm is null)
            {
                throw new ArgumentNullException(nameof(parm));
            }

            var response = GetRequestResult(parm, postParamDict);

            return(JsonConvert.DeserializeObject <T>(response));
        }
Пример #3
0
        /// <summary>
        ///     Uploads the file.
        /// </summary>
        /// <param name="parm"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string UploadFile(this ResponseParam parm, FileSystemInfo filePath = null)
        {
            if (parm is null)
            {
                throw new ArgumentNullException(nameof(parm));
            }

            var response = parm.CreateHttpResponse(string.Empty, filePath);

            var stream = response.GetResponseStream();

            if (stream is null)
            {
                throw new NullReferenceException(nameof(stream));
            }

            using (var reader = new StreamReader(stream, parm.EncodeType))
                return(reader.ReadToEnd());
        }
Пример #4
0
        /// <summary>
        ///     Downloads the file.
        /// </summary>
        /// <param name="parm"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static byte[] DownloadFile(this ResponseParam parm, FileSystemInfo filePath = null)
        {
            if (parm is null)
            {
                throw new ArgumentNullException(nameof(parm));
            }

            var response = parm.CreateHttpResponse(string.Empty, filePath);

            var st = response.GetResponseStream();

            var results = new byte[response.ContentLength];

            if (st is null)
            {
                return(results);
            }

            st.Read(results, 0, results.Length);
            st.Close();

            return(results);
        }
Пример #5
0
        /// <summary>
        ///     Creates http request data.
        /// </summary>
        /// <param name="parm"></param>
        /// <param name="postData"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static HttpWebResponse CreateHttpResponse(this ResponseParam parm, string postData = null, FileSystemInfo filePath = null)
        {
            if (parm is null)
            {
                throw new ArgumentNullException(nameof(parm));
            }

            HttpWebRequest request;

            if (parm.Url.StartsWith(@"https:\\", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = CheckResultValidation;
                request = WebRequest.Create(parm.Url) as HttpWebRequest;

                if (request != null)
                {
                    request.ProtocolVersion = HttpVersion.Version10;
                }
            }

            else
            {
                request = WebRequest.Create(parm.Url) as HttpWebRequest;
            }

            if (request is null)
            {
                throw new NullReferenceException(nameof(request));
            }

            if (parm.Proxy != null)
            {
                request.Proxy = parm.Proxy;
            }

            switch (parm.Type)
            {
            case RequestType.Post:
                request.Method = "POST";

                break;

            case RequestType.Delete:
                request.Method = "DELETE";

                break;

            case RequestType.Put:
                request.Method = "PUT";

                break;

            case RequestType.Patch:
                request.Method = "PATCH";

                break;

            case RequestType.Get:
                request.Method = "GET";

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            request.Accept = "text/html, application/xhtml+xml, application/json, text/javascript, */*; q=0.01";

            request.Headers["Accept-Language"] = "en-US,en;q=0.5";

            request.Headers["Pragma"] = "no-cache";

            request.Headers.Add("X-Requested-With", "XMLHttpRequest");

            request.Headers.Add("x-authentication-token", parm.Token);

            request.Headers.Add("X-CORAL-TENANT", parm.TenantId);

            request.Headers.Add("X-AUTH-ID", parm.AuthId);

            request.Headers.Add("X-Authorization", parm.Authorization);

            request.Referer = parm.Referer;

            request.UserAgent = parm.UserAgent;

            request.ContentType = parm.ContentType;

            if (parm.Cookies != null)
            {
                request.CookieContainer = new CookieContainer();

                request.CookieContainer.Add(parm.Cookies);
            }
            else
            {
                request.CookieContainer = new CookieContainer();

                request.CookieContainer.Add(Cookies);
            }

            if (parm.Timeout.HasValue)
            {
                request.Timeout = parm.Timeout.Value * 1000;
            }

            request.Expect = string.Empty;

            if (!string.IsNullOrEmpty(filePath?.FullName))
            {
                SetFileStream(filePath, request);
            }

            if (!string.IsNullOrEmpty(postData))
            {
                var data = parm.EncodeType.GetBytes(postData);

                using (var stream = request.GetRequestStream())
                    stream.Write(data, 0, data.Length);
            }

            if (!(request.GetResponse() is HttpWebResponse result))
            {
                throw new NullReferenceException(nameof(result));
            }

            Cookies.Add(request.CookieContainer.GetCookies(new Uri(@"http:\\" + new Uri(parm.Url).Host)));

            Cookies.Add(request.CookieContainer.GetCookies(new Uri(@"https:\\" + new Uri(parm.Url).Host)));

            Cookies.Add(result.Cookies);

            return(result);
        }