예제 #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 void HttpGet(
            SqlString url,
            SqlXml headerXml,
            out SqlBoolean success,
            out SqlString response,
            out SqlString error)
        {
            List <KeyValuePair <string, string> > headers = ClrHttp.GetHeaders(headerXml);
            HttpResult httpResult = HttpLayer.Get(url.IsNull ? (string)null : url.Value, (IEnumerable <KeyValuePair <string, string> >)headers);

            success  = (SqlBoolean)httpResult.Success;
            response = (SqlString)httpResult.Response;
            error    = (SqlString)httpResult.Error;
        }
예제 #3
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);
        }