public static string SendViaSocket5(TelegramParams TelegramParams, string text) { try { using (var request = new HttpRequest()) { request.Proxy = Socks5ProxyClient.Parse(TelegramParams.Socks5Proxy.IP + ":" + TelegramParams.Socks5Proxy.Port); if (!String.IsNullOrEmpty(TelegramParams.Socks5Proxy.Login)) { request.Proxy.Username = TelegramParams.Socks5Proxy.Login; request.Proxy.Password = TelegramParams.Socks5Proxy.Password; } return(request.Get("https://api.telegram.org/bot426158475:AAHwCjhlnvNOXlaanVy5Tja3W5h6QySxK_g/sendMessage?chat_id=" + TelegramParams.ChatId + "&text=" + text).ToString()); } } catch (HttpException ex) { return(Variables.requestStateError); } }
public static string SendTelegram(TelegramParams TelegramParams, string text) { Encoding enc = Encoding.GetEncoding("UTF-8"); string strASCII = string.Empty; byte[] bytes = enc.GetBytes(text); foreach (var byt in bytes) { strASCII = strASCII + String.Format("%{0:X2}", byt); } text = strASCII; if (TelegramParams.Socks5Proxy != null) { return(Socks5.SendViaSocket5(TelegramParams, text)); } else { return(HTTPRequests.HTTPRequest(TelegramParams, "GET", "https://api.telegram.org/bot426158475:AAHwCjhlnvNOXlaanVy5Tja3W5h6QySxK_g/sendMessage?chat_id=" + TelegramParams.ChatId + "&text=" + text, String.Empty)); } }
public static string HTTPRequest(TelegramParams TelegramParams, string typeRequest, string request, string data) { //AdditionalFunc.Trace("Request: \n" + request + "\n\n" + "Data:\n" + data + "\n\n"); try { ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; CredentialCache cache = new CredentialCache(); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(request); if (TelegramParams != null) { req.Proxy = new WebProxy(TelegramParams.Proxy.IP, TelegramParams.Proxy.Port); //req.Proxy = new WebProxy("http://" + TelegramParams.Proxy.IP + ":" + TelegramParams.Proxy.Port + "/", true); if (!String.IsNullOrEmpty(TelegramParams.Proxy.Login) & !String.IsNullOrEmpty(TelegramParams.Proxy.Password)) { req.Proxy.Credentials = new NetworkCredential(TelegramParams.Proxy.Login, TelegramParams.Proxy.Password); } } //Игнорируем недостоверный сертификат SSL ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return(true); }; req.KeepAlive = false; req.PreAuthenticate = true; req.Method = typeRequest; req.Timeout = 10000; if (!String.Equals(typeRequest, Variables.get)) { byte[] byteArray = Encoding.UTF8.GetBytes(data); req.ContentType = "application/json"; req.ContentLength = byteArray.Length; Stream dataStreamReq = req.GetRequestStream(); dataStreamReq.Write(byteArray, 0, byteArray.Length); dataStreamReq.Close(); } WebResponse response = req.GetResponse(); Stream dataStreamResp = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStreamResp, Encoding.UTF8); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStreamResp.Close(); response.Close(); //AdditionalFunc.Trace("Responce: \n" + responseFromServer + "\n\n\n"); return(responseFromServer); } catch (Exception ex) { AdditionalFunc.Trace("Request: \n" + request + "\n\n" + "Data:\n" + data); AdditionalFunc.Trace("Responce: \n" + ex.ToString()); return(Variables.requestStateError); } }