Java http客户端使用JIRA REST API,我收到java.net.ConnectException:连接被拒绝:连接

时间:2018-04-19 08:27:51

标签: jira-rest-api

下面是我使用的代码,不确定我是否做错了什么并且卡在这里......

try {// Object jsonMessage = arg0.getMessage()。getPayload();

        URL url = new URL("https://jira.mycomp.com/rest/api/latest/issue");

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

        BASE64Encoder enc = new sun.misc.BASE64Encoder();
        String userpassword = "uname" + ":" + "pwd";
        String encodedAuthorization = enc.encode(userpassword.getBytes());
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
        //conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

        conn.setRequestMethod("GET");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write("JIRA-ID");
        wr.flush();

        StringBuilder sb = new StringBuilder();
        int HttpResult = conn.getResponseCode();
        if (HttpResult == HttpsURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();
            System.out.println("" + sb.toString());
        } else {
            System.out.println(conn.getResponseMessage());
        }
    }catch(IOException e){
        e.printStackTrace();
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

并获得以下异常

java.net.ConnectException:拒绝连接:连接     at java.net.DualStackPlainSocketImpl.connect0(Native Method)     在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)     在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)     at 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)     at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)     at 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)     at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)     在sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105)     at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999)     at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)     at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1283)     at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1258)     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)

1 个答案:

答案 0 :(得分:0)

您可以使用HttpURLConnection Spring创建自己的RestTemplate客户端来调用REST,而不是使用JIRA REST APIs。 。您可以从StackOverflow上的另一个答案中引用我的示例代码。

Java Program to fetch custom/default fields of issues in JIRA

希望这有助于通过完全不同的方法回答您的问题。请向我提供您对我的代码的任何反馈(我希望进一步发展)。