救救我!我有一个Restful API链接,我使用Postman工具调用成功,但是当我在Eclipse上编译一个Restful客户端时,它失败了。我尝试关闭代理服务器,但未成功。
public class JersyGetClient {
public static void main(String[] args) {
String url = "https://abc.custhelp.com/services/rest/connect/v1.4/accounts";
String name = "thai.hoangan";
String password = "123456a@";
String authString = name + ":" + password;
String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
System.out.println("Base64 encoded auth string: " + authStringEnc);
Client restClient = Client.create();
WebResource webResource = restClient.resource(url);
ClientResponse resp = webResource.accept("application/json")
.header("Authorization", "Basic " + authStringEnc)
.header("OSvC-CREST-Application-Context", "asd")
.get(ClientResponse.class);
if (resp.getStatus() != 200) {
System.err.println("Unable to connect to the server");
}
String output = resp.getEntity(String.class);
System.out.println("response: " + output);
}
}
输出:
线程“ main”中的com.sun.jersey.api.client.ClientHandlerException异常:
java.net.ConnectException:连接超时:connect 在com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155) 在com.sun.jersey.api.client.Client.handle(Client.java:652) 在com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) 在com.sun.jersey.api.client.WebResource.access $ 200(WebResource.java:74) 在com.sun.jersey.api.client.WebResource $ Builder.get(WebResource.java:509) 在JersyGetClient.main(JersyGetClient.java:22) 原因:java.net.ConnectException:连接超时:connect 在java.net.DualStackPlainSocketImpl.connect0(本机方法) 在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) 在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 在java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 在java.net.Socket.connect(Socket.java:589) 在sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668) 在sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173) 在sun.net.NetworkClient.doConnect(NetworkClient.java:180) 在sun.net.www.http.HttpClient.openServer(HttpClient.java:432) 在sun.net.www.http.HttpClient.openServer(HttpClient.java:527) 在sun.net.www.protocol.https.HttpsClient。(HttpsClient.java:264) 在sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367) 在sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) 在sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105) 在sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999) 在sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) 在sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1513) 在sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) 在java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) 在sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338) 在com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:253) 在com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153) ...还有5个
答案 0 :(得分:0)
哦,我解决了,只需添加:
System.setProperty("https.proxyHost", "proxytest.vcb");
System.setProperty("https.proxyPort", "8080");