java远程调用对方接口
在实际的项目开发中,经常需要调用远程接口,那java是如何实现调用远程接口的呢?这里主要介绍java调用远程接口的两种方式:
一、调用远程http接口
通过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; }
二、调用远程web service 接口
使用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; }
三、http接口和web service 接口的区别
常见的api接口有两类:http接口和webservice接口。
- http接口走http协议,通过路径来区分调用方法,请求报文一般是key-value形式的,返回报文一般是json串,常用的是get和post方法来请求。
- webservice接口走的soap协议,通过http传输,请求报文和返回报文都是xml格式的
封装一个java调用远程接口工具类
java调用远程接口的工具类
import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.net.httpurlconnection; import java.net.url; import com.alibaba.fastjson.jsonobject; public class remotedemo { //远程调用接口 /** * * @param url:远程接口url * @param timeout:连接超时时间 * @param object:如所需参数是json对象类型,先将参数封装成jsonobject类型 * @param method:发送方式:以post还是get * @param contenttype:指定http的content-type类型 * @return 返回的是接口返回内容对应的json字符串 */ public static string remotejsonrequest(string url, int timeout, jsonobject object, string method, string contenttype) { url connect; stringbuffer data = new stringbuffer(); try { connect = new url(url); httpurlconnection connection = (httpurlconnection) connect.openconnection(); connection.setrequestmethod(method); connection.setdooutput(true); connection.setreadtimeout(timeout); connection.setrequestproperty("content-type", contenttype); outputstreamwriter paramout = new outputstreamwriter(connection.getoutputstream(), "utf-8"); paramout.write(object.tostring()); paramout.flush(); inputstream inputstream = connection.getinputstream(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream, "utf-8"); bufferedreader reader = new bufferedreader(inputstreamreader); string line; while ((line = reader.readline()) != null) { data.append(line); } paramout.close(); reader.close(); } catch (exception e) { e.printstacktrace(); } return data.tostring(); } }
适用场景
开发过程中离不开直接通过后端访问某些接口来拿数据,比如需要和另外的平台进行数据的交互或者对接
我们就会去参考对方给的接口文档进行数据访问,这时就可以使用这个工具类,相对还是比较方便的
使用方法简述
- 接口传参方式
接口所需的是普通键值对:直接跟在url后面进行传参,obj参数传一个空的jsonobject对象,demo如下:
jsonobject obj = new jsonobject(); remotedemo.remotejsonrequest("http://localhost:8080/xx/xx?k1=v1&k2=v2", 5000, obj, "post" ,"application/x-www-form-urlencoded");
接口所需的是json字符串:则将参数封装在jsonobject对象里面,demo如下:
jsonobject obj = new jsonobject(); obj.put("k1", "v1"); obj.put("k2", "v2"); remotedemo.remotejsonrequest("http://localhost:8080/xx/xx", 5000, obj, "post", "application/json");
另外就是参数method和参数contenttype,method是指定以post方式还是get方式访问请求,这里要求大写,传字符串"post"或者"get";contenttype参数用来指定http对应的content-type内容,一般对应的接口文档会给出,这里我们也需要进行明确指定。
顺便聊一聊另一个参数jsonobject,可以很好的用来进行对象和json字符串的转换,通过导的包可以看出,使用的是阿里的;因为使用阿里的效率更高,运行速度更快;而且jsonobject这部分内容低版本有漏洞,所以需要使用尽可能高的版本。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论