远程接口调用

落日映苍穹つ 2022-09-04 03:44 372阅读 0赞
调用第三方接口简单测试
  1. import java.io.*;
  2. import java.net.URL;
  3. import java.net.URLConnection;
  4. import java.net.URLDecoder;
  5. import java.net.URLEncoder;
  6. /**
  7. * @类名: RequestUtils
  8. * @说明: TODO
  9. * @作者:
  10. * @时间: 2021/8/3 16:17
  11. * @版本:
  12. */
  13. public class RequestUtils {
  14. // 对URL encodeData 进行编码
  15. public static String enCode(String code){
  16. String encodeData = null;
  17. try {
  18. encodeData = URLEncoder.encode(code, "UTF-8")
  19. .replaceAll("\\+", "%20")
  20. .replaceAll("\\!", "%21")
  21. .replaceAll("\\'", "%27")
  22. .replaceAll("\\(", "%28")
  23. .replaceAll("\\)", "%29")
  24. .replaceAll("\\~", "%7E");
  25. } catch (
  26. UnsupportedEncodingException e) {
  27. e.printStackTrace();
  28. }
  29. System.out.println(code+":"+encodeData); // 结果为:https%3A%2F%2Fwww.baidu.com%2F
  30. return encodeData;
  31. }
  32. // 对URL String 进行解码
  33. public static String enCodej(String code){
  34. // 对URL进行解码
  35. String decodeData = null;
  36. try {
  37. decodeData = URLDecoder.decode(code, "UTF-8");
  38. } catch (UnsupportedEncodingException e) {
  39. e.printStackTrace();
  40. }
  41. System.out.println(decodeData);
  42. return decodeData;
  43. }
  44. /*
  45. * @名称:远程发送POST请求
  46. * @时间:2021/8/3 16:25
  47. * @参数: 请求路径、 参数 参数可为null 例:&a=11
  48. * @返回:String
  49. **/
  50. public static String requestPost(String url,String value){
  51. //请求
  52. OutputStreamWriter out = null ;
  53. BufferedReader in = null;
  54. StringBuilder result = new StringBuilder();
  55. try {
  56. URL realUrl = new URL(url);
  57. // 打开和URL之间的连接
  58. URLConnection conn = realUrl.openConnection();
  59. //设置通用的请求头属性
  60. conn.setRequestProperty("accept", "*/*");
  61. conn.setRequestProperty("connection", "Keep-Alive");
  62. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  63. // 发送POST请求必须设置如下两行 否则会抛异常(java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true))
  64. conn.setDoOutput(true);
  65. conn.setDoInput(true);
  66. //获取URLConnection对象对应的输出流并开始发送参数
  67. out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  68. //添加参数
  69. out.write(value);
  70. out.flush();
  71. in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
  72. String line;
  73. while ((line = in.readLine()) != null) {
  74. result.append(line);
  75. }
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }finally {// 使用finally块来关闭输出流、输入流
  79. try {
  80. if (out != null) {
  81. out.close();
  82. }
  83. if (in != null) {
  84. in.close();
  85. }
  86. } catch (IOException ex) {
  87. ex.printStackTrace();
  88. }
  89. }
  90. System.out.println(result.toString());
  91. return result.toString();
  92. }
  93. public static void main(String[] args) {
  94. String code = enCode("摩羯座");
  95. //String url = "http://web.juhe.cn:8080/constellation/getAll?consName=%E5%8F%8C%E9%B1%BC%E5%BA%A7&type=today";
  96. String url = "http://web.juhe.cn:8080/constellation/getAll?consName="+code+"&type=today";
  97. String url2 = "http://web.juhe.cn:8080/constellation/getAll?consName=%E5%8F%8C%E9%B1%BC%E5%BA%A7&type=today";
  98. String url3 = "http://web.juhe.cn:8080/constellation/getAll?consName=%E5%8F%8C%E9%B1%BC%E5%BA%A7&type=today&key=6ac43fb03010950ff20a391c2395f8d0";
  99. requestPost(url,"key=6ac43fb03010950ff20a391c2395f8d0");
  100. requestPost(url2,"");
  101. requestPost(url3,"");
  102. }
  103. }

结果
在这里插入图片描述
第三方免费API申请

发表评论

表情:
评论列表 (有 0 条评论,372人围观)

还没有评论,来说两句吧...

相关阅读

    相关 dubbo远程调用接口

    最近做了一个项目需要使用dubbo调用远程接口,写出来分享一下 首先需要提供接口的人给你接口地址,注册中心地址,以及参数类型,参数类型可以是实体类,可以让客户直接把实体类发给