在实际的项目开发中,经常需要调用远程接口,那java是如何实现调用远程接口的呢?这里猿君主要介绍java调用远程接口的两种方式:
通过jdk的网络类Java.net.HttpURLConnection调用第三方http接口
/**
* @Auther kezf
* @Date 2020/4/11
* @param urlStr 目标地址
* @param json 请求参数
* @param charset 编码
* @return
*/
public static String doPost(String urlStr,String json,String charset) {
String result = null;
System.out.println("请求参数:"+json);
try {
//获取目标地址
URL url = new URL(urlStr);
//创建连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置向HttpURLConnection输出、输入(发送数据、接收数据),当请求为post时必须设置这两个参数
connection.setDoOutput(true);
connection.setDoInput(true);
//设置请求方式
connection.setRequestMethod("POST");
//设置是否开启缓存,post请求时,缓存必须关掉
connection.setUseCaches(false);
//设置连接是否自动处理重定向(setFollowRedirects:所用http连接;setInstanceFollowRedirects:本次连接)
connection.setInstanceFollowRedirects(true);
//设置提交内容类型
connection.setRequestProperty("Content-Type","application/json");
//开始连接
connection.connect();
//发送请求
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(json.getBytes(charset));
out.flush();
out.close();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), charset));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes());
sb.append(lines);
}
result = sb.toString();
System.out.println("请求返回结果:"+result);
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
使用axis的call方式调用
public String signConfirm(TpgDsfxyxx dsfxyqdxx){
String result=null;
try {
//创建服务
Service service = new Service();
//创建Call对象
Call call = (Call) service.createCall();
//设置服务所在的地址
call.setTargetEndpointAddress(StringUtil.getGzfconfigValueByName("dsfxy_qd_url"));
//设置调用方法名
call.setOperation("SignConfirm");
//添加请求参数
call.addParameter("xyh", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("sqh", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("yzm", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("sfywzl", XMLType.XSD_INTEGER, ParameterMode.IN);
call.addParameter("jldxhh", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("jldxhm", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("zjlx", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("zjhm", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("dhhm", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("dz", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("fkryhdm", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("fkrkhh", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("fkrzh", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("fkrmc", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("fkrlx", XMLType.XSD_INTEGER, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
System.out.println(dsfxyqdxx.toString());
//调用服务
result = (String) call.invoke(new Object[]{String.valueOf(dsfxyqdxx.getId()), dsfxyqdxx.getSqh(), dsfxyqdxx.getYzm(),
dsfxyqdxx.getSfywzl(), dsfxyqdxx.getJldxhh(), dsfxyqdxx.getJldxhm(), dsfxyqdxx.getZjlx(),
dsfxyqdxx.getZjhm(), dsfxyqdxx.getDhhm(), dsfxyqdxx.getDz(), dsfxyqdxx.getFkryhdm(),
dsfxyqdxx.getFkrkhh(), dsfxyqdxx.getFkrzh(), dsfxyqdxx.getFkrmc(), dsfxyqdxx.getFkrlx()});
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("signConfirm###:" + result);
return result;
}
常见的API接口有两类:http接口和webservice接口。