PDA

View Full Version : سوال: اتصال به تلگرام با api-id , api-hash از طریق سی شارپ



vahidjafarzadeh
سه شنبه 15 خرداد 1397, 13:16 عصر
به عرض سلام خدمت دوستان عزیز من با استفاده از کتابخانه tlsharp قصد دارم چک کنم که یک شماره در تلگرام قبلا ثبت شده یا نه
برای این کار قطعه کد زیر را ثبت کردم ( برای انجام مراحل بعدی مشکلی ندارم ) ولی وقتی برنامه را اجرا می کنم با خطا مواجه می شم کسی هست که بدونه دلیل این خطا چیه ؟
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TLSharp.Core;

namespace ConsoleApplication4
{
class Program
{
private static int apiID = ------;
private static string apiHash = "**********************";
static void Main(string[] args)
{
Task.Run(()=>TestUserInTelegramRegister());
Console.ReadLine();
}

static async Task TestUserInTelegramRegister()
{
var store = new FileSessionStore();
try
{
var client = new TelegramClient(apiID, apiHash, store, "session", null);
await client.ConnectAsync();
}
catch (Exception e)
{
Console.WriteLine(e);

}


}
}
}





<p style="color:red">

error:
&nbsp;
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 149.154.175.100:443
</p>





148332

محمد رضا فاتحی
سه شنبه 15 خرداد 1397, 14:54 عصر
فیلترشکنتون رو روشن کنید ببینید باز هم این خطا میاد؟

vahidjafarzadeh
چهارشنبه 16 خرداد 1397, 08:59 صبح
آره فری گیت رو نصب کردم روشن می کنم بازم همین خطا رو میده

hpprogrammer
چهارشنبه 18 مهر 1397, 00:17 صبح
سلام
من هم دقیقا همین مشکل رو دارم
قبل از این که تلگرام فیلتر بشه برنامه کار میکرد ولی الان با فیلتر شکن هم کار نمیکنه.
فکر کنم برای اتصال باید از پروکسی استفاده کرد.
کسی نحوه اتصال با پروکسی رو تو tlsharp میدونه؟

vahidjafarzadeh
چهارشنبه 18 مهر 1397, 05:50 صبح
سلام دوستان ی راه حل براش پیدا کردم
با استفاده از قطعه که زیر مشکل حل خواهد شد .



TcpClient tcp = conectarProxy(httpProxyHost, httpProxyPort);
client = new TelegramClient(appId, appHash, null, null, new TcpClientConnectionHandler(tcp);

private static TcpClient conectarProxy(string httpProxyHost, int httpProxyPort)
{
var url = "http://" + httpProxyHost + ":" + httpProxyPort;
var proxyUrl = WebRequest.DefaultWebProxy.GetProxy(new Uri(url));
WebResponse response = null;
var tentativas = 10;

while (tentativas >= 0)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = true;
var webProxy = new WebProxy(proxyUrl);
request.Proxy = webProxy;
request.Method = "CONNECT";
request.Timeout = 3000;

tentativas--;
try
{
response = request.GetResponse();
break;
}
catch (Exception ex)
{
if (tentativas >= 0 && ex.Message.Equals("The operation has timed out", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("Ocorreu timeout ao tentar se conectar pelo proxy.");
}
else
{
throw new Exception("Algo deu errado", ex);
}
}
}
var responseStream = response.GetResponseStream();
Debug.Assert(responseStream != null);

const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;

var rsType = responseStream.GetType();
var connectionProperty = rsType.GetProperty("Connection", Flags);

var connection = connectionProperty.GetValue(responseStream, null);
var connectionType = connection.GetType();
var networkStreamProperty = connectionType.GetProperty("NetworkStream", Flags);

var networkStream = networkStreamProperty.GetValue(connection, null);
var nsType = networkStream.GetType();
var socketProperty = nsType.GetProperty("Socket", Flags);
var socket = (Socket)socketProperty.GetValue(networkStream, null);

return new TcpClient { Client = socket };
}





client = new TelegramClient(appId, appHash, null, null, new TcpClientConnectionHandler(tcp));

yusef ghobadi
شنبه 24 آذر 1397, 18:26 عصر
سلام دوستان ی راه حل براش پیدا کردم
با استفاده از قطعه که زیر مشکل حل خواهد شد .



TcpClient tcp = conectarProxy(httpProxyHost, httpProxyPort);
client = new TelegramClient(appId, appHash, null, null, new TcpClientConnectionHandler(tcp);

private static TcpClient conectarProxy(string httpProxyHost, int httpProxyPort)
{
var url = "http://" + httpProxyHost + ":" + httpProxyPort;
var proxyUrl = WebRequest.DefaultWebProxy.GetProxy(new Uri(url));
WebResponse response = null;
var tentativas = 10;

while (tentativas >= 0)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = true;
var webProxy = new WebProxy(proxyUrl);
request.Proxy = webProxy;
request.Method = "CONNECT";
request.Timeout = 3000;

tentativas--;
try
{
response = request.GetResponse();
break;
}
catch (Exception ex)
{
if (tentativas >= 0 && ex.Message.Equals("The operation has timed out", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("Ocorreu timeout ao tentar se conectar pelo proxy.");
}
else
{
throw new Exception("Algo deu errado", ex);
}
}
}
var responseStream = response.GetResponseStream();
Debug.Assert(responseStream != null);

const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;

var rsType = responseStream.GetType();
var connectionProperty = rsType.GetProperty("Connection", Flags);

var connection = connectionProperty.GetValue(responseStream, null);
var connectionType = connection.GetType();
var networkStreamProperty = connectionType.GetProperty("NetworkStream", Flags);

var networkStream = networkStreamProperty.GetValue(connection, null);
var nsType = networkStream.GetType();
var socketProperty = nsType.GetProperty("Socket", Flags);
var socket = (Socket)socketProperty.GetValue(networkStream, null);

return new TcpClient { Client = socket };
}





client = new TelegramClient(appId, appHash, null, null, new TcpClientConnectionHandler(tcp));

این تابع خروجی اش از نوع TcpClient هست ولی ورودی TelegramClient از نوع TcpClientConnectionHandler هست و خطا میده در این خط روی شی tcp
client = new TelegramClient(appId, appHash, null, null, new TcpClientConnectionHandler(tcp));[/QUOTE]

rasam2018
دوشنبه 26 آذر 1397, 10:05 صبح
ممنون از انجمن عالی تون.مطالبش بسیار کامل و جامع هست.موفق باشید.




اگر علاقه مند به مباحث مربوط به طراحی و بهینه سازی وب سایت هستید از پیج های ما بازدید کنید:
طراحی سایت در تبریز (http://www.rasamweb.com/web-design-in-tabriz/)
طراحی اپلیکیشن در تبریز (http://www.rasamweb.com/design-applications-in-tabriz/)
طراحی لوگو در تبریز (http://www.rasamweb.com/%D8%B7%D8%B1%D8%A7%D8%AD%DB%8C-%D9%84%D9%88%DA%AF%D9%88-%D8%AA%D8%A8%D8%B1%DB%8C%D8%B2/)
سئو در تبریز (http://www.rasamweb.com/%D8%A8%D9%87%DB%8C%D9%86%D9%87-%D8%B3%D8%A7%D8%B2%DB%8C-%D8%B3%D8%A7%DB%8C%D8%AA-%D8%AF%D8%B1-%D8%A2%D8%B0%D8%B1%D8%A8%D8%A7%DB%8C%D8%AC%D8%A7%D 9%86-%D8%B4%D8%B1%D9%82%DB%8C/)