API接口说明文档传送门
Confluence的REST API访问一样采用http访问的形式,但最主要的是接口的鉴权,本文使用的是用户名+密码的鉴权方式。当然Confluence还提供令牌的鉴权方式,只是需要自己通过界面申请。闲话少叙,直接放代码
public class Test {public static void main(String[] args) throws UnsupportedEncodingException {//用户名String name = "xxxxxx";//密码String password = "xxxxxxx";String authString = name + ":" + password;byte[] authEncBytes = Bytes("utf-8"));String authStringEnc = new String(authEncBytes);//注意加空格System.out.println("Basic " + authStringEnc);}
}
使用PostMan测试
public class Test {private static String path = "xxxxxxxxxxx";public static void main(String[] args) throws UnsupportedEncodingException {//用户名String name = "xxxxxxx";//密码String password = "xxxxxxxx";String authString = name + ":" + password;byte[] authEncBytes = Bytes("utf-8"));String authStringEnc = new String(authEncBytes);//注意加空格System.out.println("Basic " + authStringEnc);BufferedReader in = null;StringBuilder result = new StringBuilder();try {URL realUrl = new URL(path);URLConnection connection = realUrl.openConnection();connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");connection.setRequestProperty("Accept", "application/json; charset=utf-8");connection.setRequestProperty("Authorization", "Basic " + authStringEnc);t();in = new BufferedReader(new InputStream(), "utf-8"));String line;while ((line = in.readLine()) != null) {result.append(line);}} catch (Exception e) {e.printStackTrace();} finally {try {if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}}System.out.String());}
}
上述代码采用的是Get提交方式,如果REST API需要Post的提交方式,可将代码中URL连接改成Post方式。当然,更推荐使用HttpClient或OkHttp等轮子。
本文发布于:2024-02-05 03:52:26,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170723412062823.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |