距离上次写mirth的博客已经好长时间了,一直找不到状态。这几天经过小永同学的帮忙,写了一个调用clientapi的实例。希望大家指导。
1,client api地址:https://121.42.147.xxx:8445/api/。是swagger搞的(一款RESTFUL接口的文档在线自动生成+功能测试功能软件,很厉害)有兴趣的同志可以研究一下。
2,接口调用,一个POST
登录接口调用地址:https://121.42.147.xxx:8445/api/users/_login
下面就是代码了
public String sendHttps(String url, String xml, String type, String method)
throws Exception {
String result = "";
try {
HttpURLConnection connection = null;
InputStream response = null;
CSAX509KeyManager[] tm = { new CSAX509KeyManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
SSLSocketFactory sslFactoryJavax = sslContext.getSocketFactory();
javax.net.ssl.HttpsURLConnection
.setDefaultSSLSocketFactory(sslFactoryJavax);
javax.net.ssl.HttpsURLConnection
.setDefaultHostnameVerifier(new ProxyHostNameVerifier());
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(300);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod(method);
connection.setRequestProperty("Content-Type", type);
connection.setRequestProperty("Content-Length",
String.valueOf(xml.length()));
connection.connect();
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print(xml);
out.flush();
out.close();
response = connection.getInputStream();
String line = null;
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(
response));
while ((line = br.readLine()) != null) {
sb.append(line);
}
result = sb.toString();
br.close();
response.close();
out.close();
connection.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
return result;
}
看起来还是挺简单的