通过另一台服务器转发Java HTTP请求

时间:2018-08-28 15:06:01

标签: java http amazon-ec2 apache-httpclient-4.x

我有两个AWS实例I1和I2,其IP地址分别为ip1和ip2。我想要实现的是:

  • 我想从I1(使用java httpclient)命中一个URL,并希望该请求应通过I2实例,即,外界应该显示该请求来自实例I2。

例如:

我正在从实例I1执行以下代码:

class Test {

    public static void main(String[] args) {

String url = "https://api.ipify.org?format=json",
       proxy = "ip2";
       Proxy p = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, 80));
 URL server = new URL(url);
Properties systemProperties = System.getProperties();
HttpURLConnection connection = (HttpURLConnection)server.openConnection(p);
connection.connect();
String theString = IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8);
 System.out.println(theString);
}
}

输出应为实例I2的公共Ip 。但这引发了异常:

Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.Socket.connect(Socket.java:589)
        at java.net.Socket.connect(Socket.java:538)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
        at sun.net.www.http.HttpClient$1.run(HttpClient.java:515)
        at sun.net.www.http.HttpClient$1.run(HttpClient.java:513)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.net.www.http.HttpClient.privilegedOpenServer(HttpClient.java:512)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:553)
        at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1199)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:162)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl$connect.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
        at com.exp.Test.main(Test.java:24)

我已经用谷歌搜索了上述用例,但找不到任何东西。任何帮助都可以申请。

1 个答案:

答案 0 :(得分:0)

此问题已解决。 在实例I2上安装了鱿鱼服务器。