在过去的一天半时间里,我一直在与这个问题作斗争,但是在我的搜索未能产生任何解决问题的方法后,我终于崩溃了。
我有一个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();
}
}
答案 0 :(得分:0)
对于遇到此问题并在其中挣扎的任何人,以下是对我有用的代码:
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>