如何在Java App中连接到Telegram Bot API时修复SocketException

时间:2019-06-12 09:16:48

标签: java proxy telegram vpn telegram-bot

我正在使用Telegram机器人。我知道,在我的国家,Telegram已被锁定,因此我决定通过代理连接到Telegram Bot API。当我尝试运行此代码时:

import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;

public class SharperBotApp {
    private static final String PROXY_HOST = "89.200.150.35";
    private static final int PROXY_PORT = 37499;//TODO

    public static void main(String[] args) {
        ApiContextInitializer.init();
        TelegramBotsApi botsApi = new TelegramBotsApi();

        DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);

        botOptions.setProxyHost(PROXY_HOST);
        botOptions.setProxyPort(PROXY_PORT);
        botOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS4);

        try {
            botsApi.registerBot(new SharperBot(botOptions));
        } catch (TelegramApiRequestException e) {
            e.printStackTrace();
        }
    }
}

我正在获取此堆栈跟踪:

Jun 12, 2019 11:56:51 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://api.telegram.org:443
Jun 12, 2019 11:56:51 AM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://api.telegram.org:443: Malformed reply from SOCKS server
Jun 12, 2019 11:56:51 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://api.telegram.org:443
Jun 12, 2019 11:56:52 AM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://api.telegram.org:443: Malformed reply from SOCKS server
Jun 12, 2019 11:56:52 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://api.telegram.org:443
org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException: Error removing old webhook
    at org.telegram.telegrambots.util.WebhookUtils.clearWebhook(WebhookUtils.java:79)
    at org.telegram.telegrambots.bots.TelegramLongPollingBot.clearWebhook(TelegramLongPollingBot.java:25)
    at org.telegram.telegrambots.meta.TelegramBotsApi.registerBot(TelegramBotsApi.java:120)
    at bot.SharperBotApp.main(SharperBotApp.java:24)
Caused by: org.telegram.telegrambots.meta.exceptions.TelegramApiException: Unable to execute deleteWebhook method
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendApiMethod(DefaultAbsSender.java:719)
    at org.telegram.telegrambots.meta.bots.AbsSender.execute(AbsSender.java:47)
    at org.telegram.telegrambots.util.WebhookUtils.clearWebhook(WebhookUtils.java:74)
    ... 3 more
Caused by: java.net.SocketException: Malformed reply from SOCKS server
    at java.net.SocksSocketImpl.readSocksReply(SocksSocketImpl.java:129)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:459)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:339)
    at org.telegram.telegrambots.facilities.proxysocketfactorys.SocksSSLConnectionSocketFactory.connectSocket(SocksSSLConnectionSocketFactory.java:41)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendHttpPostRequest(DefaultAbsSender.java:765)
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendMethodRequest(DefaultAbsSender.java:761)
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendApiMethod(DefaultAbsSender.java:716)
    ... 5 more

Process finished with exit code 0

我搜索了它,发现人们建议使用代理...但是我已经做到了!所以我决定使用VPN。我在笔记本电脑上安装了Windscribe,但也遇到了此异常。我应该怎么做?也许我做错了什么? (我的笔记本电脑上安装了Debian 9。)

1 个答案:

答案 0 :(得分:1)

我明白了。解决方案是显而易见的。 您只需要使用tor代理即可:

public static void main(String[] args) {
        System.getProperties().put("proxySet", true);
        System.getProperties().put("socksProxyHost", 127.0.0.1);
        System.getProperties().put("socksProxyPort", 9050);
}

但是以前,您应该安装Tor服务(而不是 tor浏览器):

sudo apt-get install tor