无法使用Java通过REST从VSTS中获取数据..总是会出现连接超时错误

时间:2019-03-06 05:12:21

标签: java tfs azure-devops

我试图通过JAVA中的REST连接VSTS,但是总是超时。使用此代码,我可以连接http,但是尝试连接https时总是出错。将在评论中发布错误。收到此错误-> com.sun.jersey.api.client.ClientHandlerException:java.net.SocketTimeoutException:连接超时

  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;
        }

    }

  }
}

1 个答案:

答案 0 :(得分:0)

根据您的描述,它似乎与您的代码无关。

我建议您可以检查TFS服务器是否存在Https绑定。

enter image description here

如果没有Https绑定,请尝试添加相应的Https绑定

enter image description here

相关问题