通过http调用外部url,可实现跨平台resultfor、爬虫等应用场景

GET 请求调用

示例如下:

  1. import java.io.IOException;
  2. import okhttp3.OkHttpClient;
  3. import okhttp3.Request;
  4. import okhttp3.Response;
  5. def run(String url) throws IOException {
  6. Request request = new Request.Builder()
  7. .url(url)
  8. .build();
  9. try (Response response = new OkHttpClient().newCall(request).execute()) {
  10. return response.body().string();
  11. }
  12. }
  13. json {
  14. baidu run("https://www.baidu.com")
  15. }

运行结果:

  1. {"baidu":"<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>\u767e\u5ea6\u4e00\u4e0b\uff0c\u4f60\u5c31\u77e5\u9053</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class=\"bg s_ipt_wr\"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class=\"bg s_btn_wr\"><input type=submit id=su value=\u767e\u5ea6\u4e00\u4e0b class=\"bg s_btn\" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>\u65b0\u95fb</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>\u5730\u56fe</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>\u89c6\u9891</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>\u8d34\u5427</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>\u767b\u5f55</a> </noscript> <script>document.write('<a href=\"http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === \"\" ? \"?\" : \"&\")+ \"bdorz_come=1\")+ '\" name=\"tj_login\" class=\"lb\">\u767b\u5f55</a>');\r\n </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style=\"display: block;\">\u66f4\u591a\u4ea7\u54c1</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>\u5173\u4e8e\u767e\u5ea6</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>\u4f7f\u7528\u767e\u5ea6\u524d\u5fc5\u8bfb</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>\u610f\u89c1\u53cd\u9988</a>&nbsp;\u4eacICP\u8bc1030173\u53f7&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n"}

POST 请求调用

示例如下:

  1. import java.io.IOException;
  2. import okhttp3.MediaType;
  3. import okhttp3.OkHttpClient;
  4. import okhttp3.Request;
  5. import okhttp3.RequestBody;
  6. import okhttp3.Response;
  7. def post(String url, String json) throws IOException {
  8. RequestBody body = RequestBody.create(json, MediaType.get("application/json; charset=utf-8"));
  9. Request request = new Request.Builder()
  10. .url(url)
  11. .post(body)
  12. .build();
  13. try (Response response = new OkHttpClient().newCall(request).execute()) {
  14. return response.body().string();
  15. }
  16. }
  17. def bowlingJson() {
  18. return '''
  19. {"records":{"car":{"name":"HSV Maloo","make":"Holden","year":2006,"country":"Australia","record":{"type":"speed","description":"production pickup truck with speed of 271kph"}}}}
  20. ''';
  21. }
  22. json {
  23. postData post("http://129.204.207.148:20001/example/web/parameter?method=testBody", bowlingJson())
  24. }

运行结果:

  1. {"postData":"[method:testBody]\n===============================\n {\"records\":{\"car\":{\"name\":\"HSV Maloo\",\"make\":\"Holden\",\"year\":2006,\"country\":\"Australia\",\"record\":{\"type\":\"speed\",\"description\":\"production pickup truck with speed of 271kph\"}}}} \n"}