动态获取代理服务器详细信息时来自 SOCKS 服务器的格式错误的回复

Malformed Reply from SOCKS Server when fetching proxy server details dynamically

我正在尝试通过代理访问网站 server.I 我正在使用 httpclient。

这是工作正常的代码:

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

public class Working {
private static  String PROXY_HOST = "proxy.test.org";
private static  int PROXY_PORT = 80;

    public static void main(String[] args) {
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod("https://www.example.org");
    HostConfiguration config = client.getHostConfiguration();
    config.setProxy(PROXY_HOST, PROXY_PORT);

    try {
          client.executeMethod(method);
            if (method.getStatusCode() == HttpStatus.SC_OK) {
           String response = method.getResponseBodyAsString();
           System.out.println("Response = " + response);
                        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }
    System.out.println("end");
    }
}

但是当我使用以下方法动态设置 PROXY_HOST 和 PROXY_PORT 值时,我得到 "Malformed reply from SOCKS server" 异常

public static void getProxyDetails()
{
    Properties systemSettings = System.getProperties();
    systemSettings.put("proxySet", "true");
    try {
        Proxy proxy = (Proxy) ProxySelector.getDefault().select(new URI(
                "https://www.example2.com/xyz")).iterator().
             next();
         System.out.println("proxy hostname : " + proxy.type());
          InetSocketAddress addr = (InetSocketAddress)proxy.address();
          if (addr == null) {
              System.out.println("No Proxy");
           } else {
              System.out.println("proxy hostname : " + addr.getHostName());
              System.out.println("proxy port : " + addr.getPort());
               PROXY_HOST = addr.getHostName();
               PROXY_PORT = addr.getPort();            
           }
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我尝试了本网站提供的其他建议,但没有解决我的问题。 我在代码中犯了什么错误吗?感谢任何帮助。

没关系。我能够通过使用 HttpURLConnection 而不是 HttpClient 来解决问题。 https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html