コード例 #1
0
        /// <summary>
        /// Gets all type request string data.
        /// </summary>
        /// <param name="param"></param>
        /// <param name="url"></param>
        /// <param name="filePath"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        public static string GetTypeRequest(this ResponseParam param, string url, string filePath = null, string postData = null)
        {
            var response = param.CreateHttpResponse(url, filePath, postData);

            using (var reader = new StreamReader(response.GetResponseStream(), param.EncodeType))
                return(reader.ReadToEnd());
        }
コード例 #2
0
ファイル: HttpAssist.cs プロジェクト: brickjia/Common
        /// <summary>
        ///     Gets all type request data.
        /// </summary>
        /// <param name="param"></param>
        /// <param name="postParamDict"></param>
        /// <returns></returns>
        public static string GetRequestResult(this ResponseParam param,
                                              IDictionary <string, string> postParamDict = null)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

            var postData = string.Empty;

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

            var response = param.CreateHttpResponse(postData);
            var stream   = response.GetResponseStream();

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

            using (var reader = new StreamReader(stream, param.EncodeType))
            {
                return(reader.ReadToEnd());
            }
        }
コード例 #3
0
        /// <summary>
        /// Uploads the file.
        /// </summary>
        /// <param name="param"></param>
        /// <param name="url"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string UploadFile(this ResponseParam param, string url, string filePath)
        {
            var response = param.CreateHttpResponse(url, filePath);

            using (var reader = new StreamReader(response.GetResponseStream(), param.EncodeType))
                return(reader.ReadToEnd());
        }
コード例 #4
0
ファイル: HttpAssist.cs プロジェクト: brickjia/Common
        /// <summary>
        ///     Gets all type request model data.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <param name="postParamDict"></param>
        /// <returns></returns>
        public static T GetRequestResult <T>(this ResponseParam param, IDictionary <string, string> postParamDict = null)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

            var response = GetRequestResult(param, postParamDict);

            return(JsonConvert.DeserializeObject <T>(response));
        }
コード例 #5
0
        /// <summary>
        /// Downloads the file.
        /// </summary>
        /// <param name="param"></param>
        /// <param name="url"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static byte[] DownloadFile(this ResponseParam param, string url, string filePath = null)
        {
            var response = param.CreateHttpResponse(url, filePath);
            var st       = response.GetResponseStream();
            var results  = new byte[response.ContentLength];

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

            return(results);
        }
コード例 #6
0
        /// <summary>
        /// Uploads the file.
        /// </summary>
        /// <param name="param"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string UploadFile(this ResponseParam param, FileSystemInfo filePath = null)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

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

            using (var reader = new StreamReader(response.GetResponseStream(), param.EncodeType))
                return(reader.ReadToEnd());
        }
コード例 #7
0
        /// <summary>
        /// Downloads the file.
        /// </summary>
        /// <param name="param"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static byte[] DownloadFile(this ResponseParam param, FileSystemInfo filePath = null)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

            var response = param.CreateHttpResponse(string.Empty, filePath);
            var st       = response.GetResponseStream();
            var results  = new byte[response.ContentLength];

            if (st != null)
            {
                st.Read(results, 0, results.Length);
                st.Close();
            }

            return(results);
        }
コード例 #8
0
ファイル: HttpAssist.cs プロジェクト: brickjia/Common
        /// <summary>
        ///     Creates http request data.
        /// </summary>
        /// <param name="param"></param>
        /// <param name="postData"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static HttpWebResponse CreateHttpResponse(this ResponseParam param, string postData = null,
                                                          FileSystemInfo filePath = null)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

            HttpWebRequest request;

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

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

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

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

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

            switch (param.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", param.Token);
            request.Headers.Add("X-CORAL-TENANT", param.TenantId);
            request.Headers.Add("X-AUTH-ID", param.AuthId);
            request.Headers.Add("X-Authorization", param.Authorization);
            request.Referer     = param.Referer;
            request.UserAgent   = param.UserAgent;
            request.ContentType = param.ContentType;

            if (param.Cookies != null)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(param.Cookies);
            }
            else
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(Cookies);
            }

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

            request.Expect = string.Empty;

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

            if (!string.IsNullOrEmpty(postData))
            {
                var data = param.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(param.Url).Host)));
            Cookies.Add(request.CookieContainer.GetCookies(new Uri(@"https:\\" + new Uri(param.Url).Host)));
            Cookies.Add(result.Cookies);

            return(result);
        }
コード例 #9
0
        /// <summary>
        /// Gets all type request model data.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <param name="url"></param>
        /// <param name="filePath"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        public static T GetTypeRequest <T>(this ResponseParam param, string url, string filePath = null, string postData = null)
        {
            var response = GetTypeRequest(param, url, filePath, postData);

            return(JsonConvert.DeserializeObject <T>(response));
        }