我正在尝试通过JAVA中的REST连接VSTS,但总是让连接超时。使用此代码,我可以使用http连接,但在尝试连接https时始终会出错。将在评论中发布错误。得到此错误 - > com.sun.jersey.api.client.ClientHandlerException:java.net.SocketTimeoutException:connect timed out
public class RESTInvoker {
public static class ConnectionFactory implements HttpURLConnectionFactory
{
SSLContext sslContext;
public ConnectionFactory() {
}
@Override
public HttpURLConnection getHttpURLConnection(URL url) throws
IOException {
initializeProxy();
HttpURLConnection con = (HttpURLConnection) url.openConnection();
if (con instanceof HttpsURLConnection) {
System.out.println("The valus is....");
HttpsURLConnection httpsCon = (HttpsURLConnection)
url.openConnection();
httpsCon.setHostnameVerifier(getHostnameVerifier());
httpsCon.setSSLSocketFactory(getSslContext().getSocketFactory());
return httpsCon;
} else {
return con;
}
}
public SSLContext getSslContext() {
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] { new
SecureTrustManager() }, new SecureRandom());
} catch (NoSuchAlgorithmException ex) {
//
Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null,
ex);
} catch (KeyManagementException ex) {
//
Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null,
ex);
}
return sslContext;
}
private HostnameVerifier getHostnameVerifier() {
return new HostnameVerifier() {
@Override
public boolean verify(String hostname, javax.net.ssl.SSLSession
sslSession) {
return true;
}
};
}
public static void main(String[] args) {
String username = "xxx";
String token = "xxx";
String tfsurl = "xxx";
String collectionName = "xxx";
String prjName = "xxxx";
byte[] encodedBytes = Base64.getEncoder().encode(token.getBytes());
System.out.println(encodedBytes);
String ab = "https://" + username + ":" + encodedBytes + "@" +
tfsurl
+ "/Inforce%20Portal/_apis/wit/wiql?api-version=4.0";
// Client client =Client.create();
System.out.println(ab);
try {
String input = "{\"query\":\"Select [System.Id], [System.Title],
[System.State] From WorkItems\"}";
URLConnectionClientHandler cc = new
URLConnectionClientHandler(new ConnectionFactory());
Client client = new Client(cc);
WebResource webResource = client.resource("https://" + username + ":" + token + "@" + tfsurl + "/"
+ prjName + "/_apis/wit/wiql?api-version=4.0");
ClientResponse response = null;
response=webResource.type("application/json").accept("application/json").post(ClientResp
onse.class,
input);
String data = "";
try {
data = response.getEntity(String.class);
System.out.println(data);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
class SecureTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public boolean isClientTrusted(X509Certificate[] arg0) {
return true;
}
public boolean isServerTrusted(X509Certificate[] arg0) {
return true;
}
}
}
}