首先创建一个httpUtil类,方便访问网页:
1 package com.jh.azkaban; 2 3 import java.io.*; 4 import java.net.HttpURLConnection; 5 import java.net.MalformedURLException; 6 import java.net.URL; 7 import java.net.URLConnection; 8 import java.util.HashMap; 9 import java.util.Iterator; 10 import java.util.Map; 11 import java.util.Set; 12 13 /** 14 * 模拟浏览器的功能 15 * 1.测试请求百度服务器 16 * */ 17 public class httpUtl 18 { 19 public static void main(String[] args) 20 { 21 httpUtl httpUtl = new httpUtl(); 22 String requestUrl = "http://www.teabig.com/train-web-head/head/users/usersUpdate.htm#top"; 23 Map<String,String> heardmap = new HashMap<>(); 24 heardmap.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36");//这里添加的请求头信息是模仿浏览器真实访问的情景--浏览器怎么访问代码就这么访问 25 Map<String,String> parammap = new HashMap<>(); 26 /*请求服务器*/ 27 // String s = httpUtl.methodGet(requestUrl, heardmap, parammap); 28 String s = httpUtl.methodPost(requestUrl, heardmap, parammap); 29 System.out.println("响应=====》"+s); 30 } 31 32 /** 33 * 34 * @param requestUrl--请求网址 35 * @param heardmap--请求头的信息:k--v 36 * @param paramsmap--请求体的信息:k--v 37 * @return 38 */ 39 public String methodGet(String requestUrl, Map<String,String> heardmap,Map<String,String> paramsmap) 40 { 41 System.out.println("=========ceshi========methodGet======"); 42 StringBuffer sb = new StringBuffer(); 43 try { 44 URL url = new URL(requestUrl); 45 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 46 /*请求头配置*/ 47 for (Map.Entry<String, String> header : heardmap.entrySet()) { 48 /*将传入的请求体的参数传入请求头的配置中*/ 49 String key = header.getKey(); 50 String value = header.getValue(); 51 connection.setRequestProperty(key,value);//请求头的配置 52 } 53 54 /*请求头--get*/ 55 connection.setDoOutput(false);//get请求不用输出 56 connection.setDoInput(true); 57 58 /*获取输入输出流*/ 59 InputStream is = connection.getInputStream(); 60 // OutputStream os = connection.getOutputStream(); 61 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); 62 // BufferedWriter Writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 63 /*os.flush(); 64 os.close();*/ 65 /*读取信息*/ 66 String line = ""; 67 while ((line = reader.readLine()) !=null) 68 { 69 // System.out.println(line); 70 sb.append(line+" "); 71 } 72 73 reader.close(); 74 } catch (Exception e) { 75 76 77 e.printStackTrace(); 78 } 79 80 return sb.toString(); 81 } 82 public String methodPost(String requestUrl, Map<String,String> heardmap,Map<String,String> paramsmap) 83 { 84 System.out.println("=========ceshi========methodPost======"); 85 StringBuffer sb = new StringBuffer(); 86 BufferedReader reader = null; 87 BufferedWriter Writer = null; 88 try { 89 URL url = new URL(requestUrl); 90 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 91 /*请求头配置*/ 92 for (Map.Entry<String, String> header : heardmap.entrySet()) { 93 /*将传入的请求体的参数传入请求头的配置中*/ 94 String key = header.getKey(); 95 String value = header.getValue(); 96 connection.setRequestProperty(key,value);//请求头的配置 97 } 98 /*获取请求体*/ 99 Iterator<Map.Entry<String, String>> iterator = paramsmap.entrySet().iterator(); 100 while (iterator.hasNext()) 101 { 102 Map.Entry<String, String> next = iterator.next(); 103 String key = next.getKey() + ""; 104 String value = next.getValue() + ""; 105 106 } 107 108 /*请求头--get*/ 109 connection.setDoOutput(true);//get请求不用输出 110 connection.setDoInput(true); 111 112 /*获取输入输出流*/ 113 OutputStream os = connection.getOutputStream(); 114 Writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 115 116 StringBuffer sb1 = new StringBuffer(); 117 Set<Map.Entry<String, String>> paramsSet = paramsmap.entrySet(); 118 for (Map.Entry<String, String> paramsEntry : paramsSet) 119 { 120 String key = paramsEntry.getKey(); 121 String value = paramsEntry.getValue(); 122 sb1.append(key+"="+value+"&"); 123 124 } 125 // System.out.println("===params=>"+sb1); 126 Writer.write(sb1.toString());//将请求体发送到服务器 127 128 Writer.flush(); 129 // Writer.close(); 130 InputStream is = connection.getInputStream(); 131 reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); 132 /*读取信息*/ 133 String line = ""; 134 while ((line = reader.readLine()) !=null) 135 { 136 // System.out.println(line); 137 sb.append(line+" "); 138 } 139 140 // reader.close(); 141 } catch (Exception e) { 142 143 144 e.printStackTrace(); 145 }finally { 146 try { 147 Writer.close(); 148 } catch (IOException e) { 149 e.printStackTrace(); 150 } 151 try { 152 reader.close(); 153 } catch (IOException e) { 154 e.printStackTrace(); 155 } 156 } 157 158 return sb.toString(); 159 } 160 161 }
链接百度服务器测试是否可以链接
接下来创建一个链接azkaban的工具类:
1 package com.jh.azkaban; 2 3 4 import com.alibaba.fastjson.JSON; 5 import com.alibaba.fastjson.JSONObject; 6 import com.alibaba.fastjson.JSONPObject; 7 8 import java.util.HashMap; 9 import java.util.Map; 10 11 public class AzkabanApi_Util 12 { 13 private String reqstr = "http://node7-2:8081";//请求网址 14 private httpUtl httpUtl = new httpUtl(); 15 16 /** 17 * 18 * @param username 19 * @param password 20 * @return 21 */ 22 public JSONObject login(String username, String password) 23 { 24 Map<String,String> heardmap = new HashMap<>(); 25 heardmap.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"); 26 heardmap.put("Content-Type","application/x-www-form-urlencoded"); 27 Map<String,String> parammap = new HashMap<>(); 28 parammap.put("action","login"); 29 parammap.put("username",username); 30 parammap.put("password",password); 31 String result = httpUtl.methodPost(reqstr, heardmap, parammap); 32 JSONObject parse = (JSONObject) JSON.parse(result); 33 return parse; 34 } 35 public JSONObject create(String sessionid, String name, String description) 36 { 37 reqstr = reqstr + "/manager"; 38 Map<String,String> heardmap = new HashMap<>(); 39 heardmap.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"); 40 heardmap.put("Content-Type","application/x-www-form-urlencoded"); 41 42 Map<String,String> parammap = new HashMap<>(); 43 parammap.put("action","create"); 44 parammap.put("session.id",sessionid); 45 parammap.put("name",name); 46 parammap.put("description",description); 47 48 String result = httpUtl.methodPost(reqstr, heardmap, parammap); 49 50 JSONObject parse = (JSONObject) JSON.parse(result); 51 return parse; 52 } 53 }
创建main方法链接并创建工作流:
1 package com.jh.azkaban; 2 3 import com.alibaba.fastjson.JSONObject; 4 import com.alibaba.fastjson.JSONPObject; 5 6 //import com.alibaba.fastjson.JSONObject; 7 /** 8 * 链接azkaban的API 9 * */ 10 public class my_azkaban 11 { 12 // private 13 public static void main(String[] args) 14 { 15 //获取azkaban对象 16 AzkabanApi_Util azkabanApi = new AzkabanApi_Util(); 17 String user = "azkaban"; 18 String pwd = "azkaban"; 19 //先登录服务器,获取sessionId 20 JSONObject jsonObjectstr = azkabanApi.login(user, pwd); 21 // jsonpObjectstr.getFunction("status"); 22 if (jsonObjectstr != null && "success".equalsIgnoreCase(jsonObjectstr.getString("status")) ) 23 { 24 String sessionid = jsonObjectstr.getString("session.id"); 25 System.out.println(sessionid); 26 27 String name = "my_flow04"; 28 String describe = "my_flow04"; 29 //通过获取的sessionId,链接并创建工作路流 30 JSONObject jsonObject = azkabanApi.create(sessionid, name, describe); 31 System.out.println(jsonObject); 32 33 34 } 35 36 } 37 }
这样,一个简单的azkaban--API链接就搞定了!!