java - Java 应用程序代理连接被拒绝
问题描述
在过去的一天半里,我一直在与这个问题作斗争,在我的搜索未能产生任何解决我的问题后终于崩溃了。
我有一个 Spring-boot 应用程序,可以对远程服务器进行 SOAP 调用。使用 STS 或在我的开发笔记本电脑上本地运行(通过 STS>Run、mvn spring-boot:run 或在本地运行 jar 文件)一切都很好。当我将代码(一个胖 jar)部署到我们的公司开发服务器时我的所有请求都因“连接被拒绝(连接被拒绝)”而失败。
为了进行分类,我在开发服务器上通过 Curl 发出了相同的请求,以查看问题出在哪里:
裸请求:
curl http://some.remote.server
--> curl: (7) 连接失败:80; 连接被拒绝
使用代理设置/授权请求:
export http_proxy=http://<proxy_user>:<proxy_pass>@<proxy_host>:<proxy_port>
curl -x $http_proxy http://some.remote.server
--> 成功!
所以在这一点上,我很确定我看到的问题是我的呼叫没有通过代理路由。
我尝试将代理设置添加到代码中,但我不喜欢降低可移植性的想法(此外,它不起作用)。
我尝试通过以下方式在命令行上设置参数:
java -Dhttp.proxyHost=<corp proxy host addr> -Dhttp.proxyPort=3128 -Dhttp.proxyUser=<proxy username> -Dhttp.proxyPassword=<proxy pass> -jar Application.jar
我所做的一切都没有什么不同。
我可以使用哪些其他工具/技术来诊断/解决我看到的这个问题?我的头发快没了拉!
[注意:最终这将在 Docker 容器中运行,但我已尽可能多地移除移动部件以简化解决方案。我认为我在做一些非常基本的错误,因为我确信这不是一个独特的问题。]
public String makeTestRequest()
{
try
{
final String authUser = PROXY_USER;
final String authPassword = PROXY_PASS;
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
System.setProperty("http.proxyUser", PROXY_USER);
System.setProperty("http.proxyPassword", PROXY_PASS);
System.setProperty("http.proxyHost", PROXY_HOST);
System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
URL url = new URL(REMOTE_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
System.out.println("Proxy? " + con.usingProxy()); // proxy?
con.setRequestMethod("GET");
int status = con.getResponseCode();
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM)
{
String location = con.getHeaderField("Location");
URL newUrl = new URL(location);
con = (HttpURLConnection) newUrl.openConnection();
}
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
content.append(inputLine);
}
// cleanup / return
in.close();
con.disconnect();
return content.toString();
}
catch (Exception e)
{
e.printStackTrace();
return e.getMessage();
}
}
解决方案
对于遇到此问题并苦苦挣扎的任何人,这是对我有用的代码:
package proxycheck;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
public class ConnectionTest
{
private static final String PROXY_HOST_URI = "mycompanyproxy.corp.net";
private static final int PROXY_PORT = 7283;
private static final String TEST_URL = "http://some.remote.service.com/something";
public static void main(String[] args)
throws IOException
{
Authenticator.setDefault(new EdlsAuthenticator());
HttpURLConnection conn;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST_URI, PROXY_PORT));
URL httpUrl = new URL(TEST_URL);
conn = (HttpURLConnection) httpUrl.openConnection(proxy);
System.out.println(conn.getResponseCode());
System.out.println(readResponse((InputStream) conn.getContent()));
}
public static String readResponse (InputStream in)
throws IOException
{
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;)
{
out.append(new String(b, 0, n));
}
return out.toString();
}
}
class EdlsAuthenticator extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
String username = "ProxyUsername";
String password = "ProxyPassword";
return new PasswordAuthentication(username, password.toCharArray());
}
}
我的 pom.xml 包含以下依赖项:
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.soulwing.ssl</groupId>
<artifactId>ssl-context-tools</artifactId>
<version>1.0.1</version>
</dependency>
推荐阅读
- python - 避免在 python 的子类中破坏 liskov 替换原则
- python - 将两个json文件合并为一个文件Python
- julia - 在 Julia 中实现涉及 if、else 和矩阵的函数的困难
- redis - 由于防火墙限制,Redis Db 连接失败
- vue.js - 为什么 beforeRouteUpdate 不更新路由
- django - 如何使用媒体 URL 将 Instagram 故事保存在我的媒体文件夹中?
- macos - 连接 AppleTV 后 Xcode 将继续运行
- arrays - 比较两个矩阵并在对应列中找到匹配的索引
- python - 如何使用熊猫将列表转换为 str?
- bash - 如何获取我的 github 存储库的最新预发布版本 - bash