如何调用第三方接口(调用第三方接口一般有什么用)

java调用第三方接口HttpClientHttpURLConnection推荐: hutool工具类HttpRequest总结

httpclientpublicvoidhttpclienttest ({//http客户端对象closeablehttpclienthttpclient=http clients.create default ); //get请求创建//http get http get=new http get (请求第三方接口的url,如3360http://127.0.0.133608082/test/find ‘ ); http get http get=new http get 快三大小单双位技巧准确率99

java调用第三方接口HttpClientHttpURLConnection推荐: hutool工具类HttpRequest总结

httpclientpublicvoidhttpclienttest ({//http客户端对象closeablehttpclienthttpclient=http clients.create default ); //get请求创建//http get http get=new http get (请求第三方接口的url,如3360http://127.0.0.133608082/test/find ‘ ); http get http get=new http get (http://localhost :18083/API/v4/clients ); //要设置请求标头,例如需要登录验证的接口,请单击, 请求标头和请求标头值’ username:password ‘登录验证的密码string encoding=datatype converter.print base64 binary (‘ username //运行httpresponse response=http client.execute (http get )//状态代码intresponsecode=response.get statusline () . if (响应代码==200 ) (/请求体httpentityentity=response.get entity ); //获取请求体内容的entity.getContent (; //在流中输入putstream input=entity.get content (; bufferedreaderbr=newbufferedreader (newinputstreamreader (input ); String str1=br.readLine (; //获取返回的json字符串string result=new string (str1. getbytes )、(utf-8 ); br.close (; input.close (; } httpurlconnectionpublicvoidhttpurlconnectiontest () {HttpURLConnection conn=null; string urlstr=’ 33603358127.0.0.133608082/test/find ‘; URL=null; try{//url地址URL的创建=newURL(urlstr ); //连接conn=(httpurlconnection ) url.openConnection ) ); //请求方式Conn.setrequestmethod(‘get ); //要设置请求标头,例如需要登录验证的接口,请单击, 请求标头和请求标头值’ username:password ‘登录验证密码string encodings=base64.get encoder (.encode tostring ) ) userng //建立连接并运行conn.connect (; //以流方式输入inputstream inputstream=conn.get inputstream (; bufferedreaderbufferedreader=newbufferedreader (newinputstreamreader ) inputstream ); //返回的json字符串strings=buffered reader.readline (; 系统. out.println (s; }catch(exceptione ) { e.printStackTrace ); } conn.disconnect (; 建议使用: Hu工具类http请求

使用hutool之前的maven引线包

ependencygroupidcn.Hu tool/groupidartifactidhutool-http/artifactidversion5.5.1/version/dependencypublicvoidhutton string resultstr=HttpRequest.get (3360http://127.0.1127.0 )要求第三方接口的url建议使用hutool工具类http request 封装在一起,使用方便。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注