示例#1
0
        public static WebResponseTemplate Send(string Method, string Link,
                                               ObservableCollection <WebRequestParamsItem> FormRequestsData = null)
        {
            var WebResponse = new WebResponseTemplate();

            // Создать объект запроса
            //var request = WebRequest.Create(Link);
            var request = (HttpWebRequest)WebRequest.Create(Link);

            request.Method = Method;
            if (Method != "GET")
            {
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                if (FormRequestsData != null)
                {
                    string RequestParams = "";

                    for (int i = 0; i < FormRequestsData.Count; i++)
                    {
                        WebRequestParamsItem ParamsItem = FormRequestsData[i];
                        if (i == FormRequestsData.Count - 1)
                        {
                            RequestParams += $"{ParamsItem.Key}={ParamsItem.Value}";
                        }
                        else
                        {
                            RequestParams += $"{ParamsItem.Key}={ParamsItem.Value}&";
                        }
                    }

                    byte[] DataStream = Encoding.UTF8.GetBytes(RequestParams);
                    request.ContentLength = DataStream.Length;

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

            try
            {
                // Получить ответ с сервера
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // Получаем поток данных из ответа
                using (var reader = new StreamReader(response.GetResponseStream()))
                    WebResponse.Response = reader.ReadToEnd();

                WebResponse.Info = GetResponseInfo(request, response);
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    using (StreamReader stream = new StreamReader(ex.Response.GetResponseStream()))
                        WebResponse.Response = stream.ReadToEnd();
                }

                WebResponse.Info = GetResponseInfo(request, ex.Response);
            }

            return(WebResponse);
        }
示例#2
0
        public static WebResponseTemplate RestRequest(string Method, string Link, ObservableCollection <WebRequestParamsItem> FormRequestsData = null)
        {
            //var Client = new RestClient(GetFirstLink(Link));
            var FinalResponse = new WebResponseTemplate();

            //IRestResponse Response = null;

            try
            {
                /*
                 * if (Method == "GET")
                 * {
                 *  var Request = new RestRequest(GetSecondLink(Link), RestSharp.Method.GET);
                 *  Response = Client.Execute(Request);
                 * }
                 * else if (Method == "POST")
                 * {
                 *  var Request = new RestRequest(GetSecondLink(Link), RestSharp.Method.POST);
                 *  Response = Client.Execute(Request);
                 * }
                 * else if (Method == "PUT")
                 * {
                 *  var Request = new RestRequest(GetSecondLink(Link), RestSharp.Method.PUT);
                 *  Response = Client.Execute(Request);
                 * }
                 * else if (Method == "PATHCH")
                 * {
                 *  var Request = new RestRequest(GetSecondLink(Link), RestSharp.Method.PATCH);
                 *  Response = Client.Execute(Request);
                 * }
                 * else if (Method == "DELETE")
                 * {
                 *  var Request = new RestRequest(GetSecondLink(Link), RestSharp.Method.DELETE);
                 *  Response = Client.Execute(Request);
                 * }
                 * else if (Method == "COPY")
                 * {
                 *  var Request = new RestRequest(GetSecondLink(Link), RestSharp.Method.COPY);
                 *  Response = Client.Execute(Request);
                 * }
                 * else if (Method == "HEAD")
                 * {
                 *  var Request = new RestRequest(GetSecondLink(Link), RestSharp.Method.HEAD);
                 *  Response = Client.Execute(Request);
                 * }
                 * else if (Method == "OPTIONS")
                 * {
                 *  var Request = new RestRequest(GetSecondLink(Link), RestSharp.Method.OPTIONS);
                 *  Response = Client.Execute(Request);
                 * }
                 * else if (Method == "LINK")
                 * {
                 *  var Request = CustomWebRequest.Send(Method, Link);
                 *  FinalResponse = Request;
                 * }
                 * else if (Method == "UNLINK")
                 * {
                 *  var Request = CustomWebRequest.Send(Method, Link);
                 *  FinalResponse = Request;
                 * }
                 * else if (Method == "PURGE")
                 * {
                 *  var Request = CustomWebRequest.Send(Method, Link);
                 *  FinalResponse = Request;
                 * }
                 * else if (Method == "LOCK")
                 * {
                 *  var Request = CustomWebRequest.Send(Method, Link);
                 *  FinalResponse = Request;
                 * }
                 * else if (Method == "UNLOCK")
                 * {
                 *  var Request = CustomWebRequest.Send(Method, Link);
                 *  FinalResponse = Request;
                 * }
                 * else if (Method == "PROPFIND")
                 * {
                 *  var Request = CustomWebRequest.Send(Method, Link);
                 *  FinalResponse = Request;
                 * }
                 * else if (Method == "VIEW")
                 * {
                 *  var Request = CustomWebRequest.Send(Method, Link);
                 *  FinalResponse = Request;
                 * }
                 *
                 * if (Response != null && Response.ErrorException != null)
                 * {
                 *  const string ErrorMessage = "Возникла ошибка при получении ответа на WEB-запрос. Ознакомьтесь с деталями в консоли.";
                 *  var Exeption = new Exception(ErrorMessage, Response.ErrorException);
                 *  //throw Exeption;
                 * }*/

                var Request = CustomWebRequest.Send(Method, Link, FormRequestsData);
                FinalResponse = Request;
            }
            catch (Exception ex)
            {
                Debug.LogError($"Возникло исключение при отправке WEB-запроса на адрес {Link}. Код ошибки:\n" + ex);
            }

            /*
             * if (Response != null)
             * {
             *  FinalResponse.Response = Response.Content;
             *  FinalResponse.Info = CustomWebRequest.GetResponseInfo(Link, Method, Response);
             *  return FinalResponse;
             * }
             * else
             * {
             *  return FinalResponse;
             * }
             */

            return(FinalResponse);
        }