0
votes

I want to call AZDO to get a TestPlan, but when I try, this is returning an IOException, and I donĀ“t get why

String testSuiteURI = "https://dev.azure.com/" +organization +"/" + project + "/_apis/testplan/Plans/" + planId + "?api-version=6.0-preview.1";
        HttpGet httpGet = new HttpGet(testSuiteURI);
        try {
            CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSSLContext(getSslContext()).build();
            httpGet.setHeader(HttpHeaders.CONTENT_TYPE,"application/json");
            httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
            //instantiate the response handler
            ResponseHandler<String> responseHandler = new MyResponseHandler();
            String response = httpClient.execute(httpGet, responseHandler);
            httpClient.close();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

Here is the stacktrace

DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: default DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection request: [route: {s}->https://dev.azure.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20] DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection leased: [id: 0][route: {s}->https://dev.azure.com:443][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20] DEBUG [org.apache.http.impl.execchain.MainClientExec] Opening connection {s}->https://dev.azure.com:443 DEBUG [org.apache.http.impl.conn.DefaultHttpClientConnectionOperator] Connecting to dev.azure.com/13.107.42.20:443 DEBUG [org.apache.http.conn.ssl.SSLConnectionSocketFactory] Connecting socket to dev.azure.com/13.107.42.20:443 with timeout 0 DEBUG [org.apache.http.impl.conn.DefaultManagedHttpClientConnection] http-outgoing-0: Shutdown connection DEBUG [org.apache.http.impl.execchain.MainClientExec] Connection discarded DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection released: [id: 0][route: {s}->https://dev.azure.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20] INFO [org.apache.http.impl.execchain.RetryExec] I/O exception (java.net.SocketException) caught when processing request to {s}->https://dev.azure.com:443: Operation not permitted DEBUG [org.apache.http.impl.execchain.RetryExec] Operation not permitted

1

1 Answers

1
votes

You can refer to this thread to call azure devops REST api with java. The following code gets a work item with devops REST api:

package com.restapi.sample;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Scanner;

import org.apache.commons.codec.binary.Base64;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ResApiMain {

    static String ServiceUrl = "https://dev.azure.com/<your_org>/";
    static String TeamProjectName = "your_team_project_name";
    static String UrlEndGetWorkItemById = "/_apis/wit/workitems/";
    static Integer WorkItemId = 1208;
    static String PAT = "your_pat";

    public static void main(String[] args) {

        try {

            String AuthStr = ":" + PAT;
            Base64 base64 = new Base64();

            String encodedPAT = new String(base64.encode(AuthStr.getBytes()));

            URL url = new URL(ServiceUrl + TeamProjectName + UrlEndGetWorkItemById + WorkItemId.toString());
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            con.setRequestProperty("Authorization", "Basic " + encodedPAT);
            System.out.println("URL - " + url.toString());
            System.out.println("PAT - " + encodedPAT);
            con.setRequestMethod("GET");

            int status = con.getResponseCode();

            if (status == 200){
                String responseBody;
                try (Scanner scanner = new Scanner(con.getInputStream())) {
                    responseBody = scanner.useDelimiter("\\A").next();
                    System.out.println(responseBody);
                }

                try {
                    Object obj = new JSONParser().parse(responseBody);
                    JSONObject jo = (JSONObject) obj;

                    String WIID = (String) jo.get("id").toString();
                    Map<String, String> fields = (Map<String, String>) jo.get("fields");
                    System.out.println("WorkItemId - " + WIID);
                    System.out.println("WorkItemTitle - " + fields.get("System.Title"));
                } catch (ParseException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }           

            con.disconnect();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}