Java通过https http协议来调用第三方接口
一、通过https协议
public static String getSqm(String nssbh){ //请求参数 String sqmParm = createSqmParm(nssbh); System.out.println(sqmParm); //返回结果 String sqmRes = ""; PrintWriter out = null; BufferedReader in = null; try { trustAllHosts(); java.net.URL realUrl = new URL("https://api.yunpiao.net/openNozzle"); HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection(); conn.setHostnameVerifier(DO_NOT_VERIFY); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setRequestProperty("Content-Type", "text/plain;charset=utf-8"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(sqmParm); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { sqmRes += line; } } catch (Exception e) { e.printStackTrace(); } finally {// 使用finally块来关闭输出流、输入流 try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return sqmRes; }
我在测试的时候曾经出错过,报错为javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
这个报错的原因是,第三方服务器不是https协议,是http服务,我换成http调用就可以了
二、http协议
public static String doBillManage(String nssbh){
String parm = createBillManageParm(nssbh);
String result = "";
PrintWriter out = null;
BufferedReader in = null;
try {
java.net.URL realUrl;
realUrl = new URL("http://locahost:8080/aa.do");
// realUrl = new URL(null,BaiWangUtil.URL,new Handler());
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("POST");
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", "text/plain;charset=utf-8");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
BASE64Encoder base64Encoder = new BASE64Encoder();
out.print(base64Encoder.encode(parm.getBytes()));
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println(result);
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] bytes = base64Decoder.decodeBuffer(result);
String res = new String(bytes, "UTF-8");
System.out.println(res);
Base64.getDecoder().decode(result.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {// 使用finally块来关闭输出流、输入流
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
