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

问题描述 投票:0回答:1

我正在尝试通过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;
        }

    }

  }
}
java tfs azure-devops
1个回答
0
投票

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

我建议您检查TFS服务器是否有Https绑定。

enter image description here

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.