2017-08-10 47 views
0

设置代理我试图发送使用电报和TLSharp的消息,而这正是我目前有:与tlsharp

static async void MainAssincrono(string[] args) 
     { 
      IniciarVariaveis(); 

      TelegramClient client; 
      TcpClient tcp = conectarProxy(httpProxyHost, httpProxyPort); 
      client = new TelegramClient(appId, appHash); 
      await client.ConnectAsync(); 

      if (habilitarClient) 
      { 
       var hashAuth = await client.SendCodeRequestAsync(apiPhone); 
       Console.WriteLine("Digite o código recebido no telegram do celular: "); 
       var code = Console.ReadLine(); 
       var user = await client.MakeAuthAsync(apiPhone, hashAuth, code); 
      } 
      else 
      { 
       var resultado = await client.GetContactsAsync(); 
       var user = resultado.users.lists.Where(x => x.GetType() == typeof(TLUser)).Cast<TLUser>().FirstOrDefault(x => x.phone == "5581971126655"); 
       await client.SendMessageAsync(new TLInputPeerUser() { user_id = user.id }, "Teste"); 
      } 

它的工作原理,但我试图建立一个代理。这里就是我想要做的事:

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 }; 
     } 

签名说,这是期待TcpClientConnectionHandler作为参数,但如果我通过TcpClientConnectionHandler这样的:

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

我给出了一个错误,说它的tpc是一个变量,但它被用作一种方法。我究竟做错了什么?

回答