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

How to fix SocketException while connecting to Telegram Bot API in Java App

我正在开发我的 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)。

您只需要使用 tor 代理即可:

public class MyApplication {
    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服务:

sudo apt-get install tor