Пример #1
0
        public static HttpResult Get(
            string url,
            IEnumerable <KeyValuePair <string, string> > headers)
        {
            HttpResult httpResult = new HttpResult();

            if (string.IsNullOrEmpty(url))
            {
                httpResult.Error = httpResult.ErrorMessage = "Invalid URL";
                return(httpResult);
            }
            try
            {
                url = String.Concat(telegram_url, url);
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol  = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "GET";
                HttpLayer.AddHeaders(req, headers);
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                    httpResult.Response = streamReader.ReadToEnd();
                httpResult.Success = response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Accepted;
            }
            catch (Exception ex)
            {
                httpResult.ErrorMessage = ex.Message;
                httpResult.Error        = ex.ToString();
            }
            return(httpResult);
        }
Пример #2
0
        public static HttpResult Post(
            string url,
            IEnumerable <KeyValuePair <string, string> > headers,
            string requestBody)
        {
            HttpResult httpResult = new HttpResult();

            if (string.IsNullOrEmpty(url))
            {
                httpResult.Error = httpResult.ErrorMessage = "Invalid URL";
                return(httpResult);
            }
            try
            {
                url = String.Concat(telegram_url, url);
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol  = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                HttpLayer.AddHeaders(req, headers);
                if (!string.IsNullOrEmpty(requestBody))
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(requestBody);
                    req.ContentLength = bytes.Length;
                    using (Stream streamWriter = req.GetRequestStream())
                    {
                        streamWriter.Write(bytes, 0, bytes.Length);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                }
                else
                {
                    req.ContentLength = 0L;
                }
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                    httpResult.Response = streamReader.ReadToEnd();
                httpResult.Success = response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Accepted;
            }
            catch (Exception ex)
            {
                httpResult.ErrorMessage = ex.Message;
                httpResult.Error        = ex.ToString();
            }
            return(httpResult);
        }