Пример #1
0
        public async Task <HttpRespVo> SendMultipart(string uri, string method,
                                                     Dictionary <string, object> formDatas, Dictionary <string, string> setHeaders = null)
        {
            var result = new HttpRespVo();

            try
            {
                var webRequest = WebRequest.Create(uri) as HttpWebRequest;
                if (webRequest == null)
                {
                    throw new InvalidCastException("This request it not a http request");
                }

                // Set method
                webRequest.Method = method;
                // Set cookie container to retrive cookies
                webRequest.CookieContainer = new CookieContainer();
                webRequest.UserAgent       = UserAgent;
                // Set headers if not null
                if (setHeaders != null)
                {
                    this.SetHttpHeaders(webRequest, setHeaders);
                }

                // Create boundary randomly and convert formdata
                string boundary    = string.Format("----------{0:N}", Guid.NewGuid());
                byte[] binFormData = this.ToMultipartFormData(formDatas, boundary);
                // Set content type
                webRequest.ContentType = ContentType + boundary;
                // Set content length
                webRequest.ContentLength = binFormData.Length;

                // Send form data to uri
                using (Stream reqStream = webRequest.GetRequestStream())
                {
                    reqStream.Write(binFormData, 0, binFormData.Length);
                }

                using (var response = await webRequest.GetResponseAsync())
                {
                    HttpWebResponse webResponse = (HttpWebResponse)response;
                    // Resp text
                    Stream       respStream = webResponse.GetResponseStream();
                    StreamReader reader     = new StreamReader(respStream, UTF8Encoding.UTF8, true);
                    string       respText   = reader.ReadToEnd();
                    result.RespText = respText;

                    // Headers
                    var headers = new Dictionary <string, string>();
                    foreach (string headerName in webResponse.Headers)
                    {
                        headers.Add(headerName, webResponse.Headers.Get(headerName));
                    }
                    result.Headers = headers;

                    // Cookies
                    var cookies = new Dictionary <string, string>();
                    foreach (Cookie cookie in webResponse.Cookies)
                    {
                        cookies.Add(cookie.Name, cookie.Value);
                    }
                    result.Cookies = cookies;

                    // StatucCode and StatucMsg
                    result.StatusCode = (int)webResponse.StatusCode;
                    result.StatusMsg  = webResponse.StatusCode.ToString();
                }
                result.IsSuccess = true;
                return(result);
            }
            catch (WebException exp)
            {
                if (exp.Response != null)
                {
                    using (var httpResponse = (HttpWebResponse)exp.Response)
                    {
                        var respStream = httpResponse.GetResponseStream();
                        var reader     = new StreamReader(respStream, UTF8Encoding.UTF8, true);

                        result.IsSuccess = false;
                        result.RespText  = await reader.ReadToEndAsync();

                        result.StatusCode = (int)httpResponse.StatusCode;
                        result.StatusMsg  = httpResponse.StatusCode.ToString();
                        return(result);
                    }
                }

                // 네트워크에 연결되어 있는지 검사한다
                bool isConnected = NetworkInterface.GetIsNetworkAvailable();
                if (isConnected == false)
                {
                    result.IsSuccess = false;
                    result.ErrorMsg  = "네트워크에 연결되어 있지 않습니다";
                    return(result);
                }

                result.StatusCode = (int)exp.Status;
                result.StatusMsg  = exp.Status.ToString();
                result.IsSuccess  = false;
                return(result);
            }
            catch (Exception exp)
            {
                result.IsSuccess = false;
                result.ErrorMsg  = exp.Message;
                return(result);
            }
        }
Пример #2
0
        public async Task <HttpRespVo> SendApplicationJson(string uri, string method, string json,
                                                           Dictionary <string, string> setHeaders)
        {
            var result = new HttpRespVo();

            try
            {
                var webRequest = WebRequest.Create(uri) as HttpWebRequest;
                if (webRequest == null)
                {
                    throw new InvalidCastException("This request it not a http request");
                }

                // Set method
                webRequest.Method = method;
                // Set cookie container to retrive cookies
                webRequest.CookieContainer = new CookieContainer();
                webRequest.UserAgent       = UserAgent;
                // Set headers if not null
                if (setHeaders != null)
                {
                    this.SetHttpHeaders(webRequest, setHeaders);
                }
                // Set content type to application/json
                webRequest.ContentType = "application/json";

                using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
                {
                    streamWriter.Write(json);
                }

                using (var response = await webRequest.GetResponseAsync())
                {
                    HttpWebResponse webResponse = (HttpWebResponse)response;
                    // Resp text
                    Stream       respStream = webResponse.GetResponseStream();
                    StreamReader reader     = new StreamReader(respStream, UTF8Encoding.UTF8, true);
                    string       respText   = reader.ReadToEnd();
                    result.RespText = respText;

                    // Headers
                    var headers = new Dictionary <string, string>();
                    foreach (string headerName in webResponse.Headers)
                    {
                        headers.Add(headerName, webResponse.Headers.Get(headerName));
                    }
                    result.Headers = headers;

                    // Cookies
                    var cookies = new Dictionary <string, string>();
                    foreach (Cookie cookie in webResponse.Cookies)
                    {
                        cookies.Add(cookie.Name, cookie.Value);
                    }
                    result.Cookies = cookies;

                    // StatucCode and StatucMsg
                    result.StatusCode = (int)webResponse.StatusCode;
                    result.StatusMsg  = webResponse.StatusCode.ToString();
                }
                result.IsSuccess = true;
                return(result);
            }
            catch (WebException exp)
            {
                if (exp.Response != null)
                {
                    using (var httpResponse = (HttpWebResponse)exp.Response)
                    {
                        var respStream = httpResponse.GetResponseStream();
                        var reader     = new StreamReader(respStream, UTF8Encoding.UTF8, true);

                        result.IsSuccess = false;
                        result.RespText  = await reader.ReadToEndAsync();

                        result.StatusCode = (int)httpResponse.StatusCode;
                        result.StatusMsg  = httpResponse.StatusCode.ToString();
                        return(result);
                    }
                }

                // 네트워크에 연결되어 있는지 검사한다
                bool isConnected = NetworkInterface.GetIsNetworkAvailable();
                if (isConnected == false)
                {
                    result.IsSuccess = false;
                    result.ErrorMsg  = "네트워크에 연결되어 있지 않습니다";
                    return(result);
                }

                result.IsSuccess = false;
                result.ErrorMsg  = exp.Message;
                return(result);
            }
            catch (Exception exp)
            {
                result.IsSuccess = false;
                result.ErrorMsg  = exp.Message;
                return(result);
            }
        }
Пример #3
0
        public async Task <HttpRespVo> Get(string uri, Dictionary <string, string> setHeaders = null)
        {
            var result = new HttpRespVo();

            try
            {
                var webRequest = (HttpWebRequest)WebRequest.Create(uri);
                webRequest.Method = "GET";
                webRequest.ServicePoint.Expect100Continue = false;
                webRequest.CookieContainer = new CookieContainer();
                webRequest.UserAgent       = UserAgent;

                // Set header
                if (setHeaders != null)
                {
                    SetHttpHeaders(webRequest, setHeaders);
                }

                using (var response = await webRequest.GetResponseAsync())
                {
                    HttpWebResponse webResponse = (HttpWebResponse)response;
                    // Resp text
                    Stream       respStream = webResponse.GetResponseStream();
                    StreamReader reader     = new StreamReader(respStream, EncodingType, true);
                    string       respText   = reader.ReadToEnd();
                    result.RespText = respText;

                    // Headers
                    var headers = new Dictionary <string, string>();
                    foreach (string headerName in webResponse.Headers)
                    {
                        headers.Add(headerName, webResponse.Headers.Get(headerName));
                    }
                    result.Headers = headers;

                    // Cookies
                    var cookies = new Dictionary <string, string>();
                    foreach (Cookie cookie in webResponse.Cookies)
                    {
                        cookies.Add(cookie.Name, cookie.Value);
                    }
                    result.Cookies = cookies;

                    // StatucCode and StatucMsg
                    result.StatusCode = (int)webResponse.StatusCode;
                    result.StatusMsg  = webResponse.StatusCode.ToString();
                }
                result.IsSuccess = true;
                return(result);
            }
            catch (WebException exp)
            {
                if (exp.Response != null)
                {
                    using (var httpResponse = (HttpWebResponse)exp.Response)
                    {
                        var respStream = httpResponse.GetResponseStream();
                        var reader     = new StreamReader(respStream);

                        result.IsSuccess = false;
                        result.RespText  = await reader.ReadToEndAsync();

                        result.StatusCode = (int)httpResponse.StatusCode;
                        result.StatusMsg  = httpResponse.StatusCode.ToString();
                        return(result);
                    }
                }

                // 네트워크에 연결되어 있는지 검사한다
                bool isConnected = NetworkInterface.GetIsNetworkAvailable();
                if (isConnected == false)
                {
                    result.IsSuccess = false;
                    result.ErrorMsg  = "네트워크에 연결되어 있지 않습니다";
                    return(result);
                }

                result.IsSuccess = false;
                result.ErrorMsg  = exp.Message;
                return(result);
            }
            catch (Exception exp)
            {
                result.IsSuccess = false;
                result.ErrorMsg  = exp.Message;
                return(result);
            }
        }