因感覺百度鷹眼的使用場景比較符合實際業務,於是對百度鷹眼做了簡單功能調試。
剛開始使用springframework封裝的RestTemplate,但是測試提示ak參數不存在。
后又試了幾種方法,均提示ak參數不存在。但是ak參數明明存在的,且是正確的(可能本人參數設置問題)。
百度相關ak參數不存在問題,發現還有一部分人遇到這個問題。
經過參考對應資料,終於把基礎的增刪改查業務調試通過。特做簡單整理,既能自我總結又能分享給萬一有需要的朋友。
1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.io.OutputStreamWriter; 4 import java.net.URL; 5 import java.net.URLConnection; 6 import org.apache.commons.io.IOUtils; 7 8 /** 9 * ClassName: YingYanUtil 10 * @Description: 鷹眼請求工具類 11 * @author ljwang 12 * @date 2017年9月4日 13 */ 14 public class YingYanUtil { 15 16 /** 17 * 百度鷹眼服務器端Key 18 */ 19 public static final String BAIDU_YINGYAN_AK_S = "*********************"; 20 21 /** 22 * 百度鷹眼服務ID 23 */ 24 public static final String BAIDU_YINGYAN_SERVICE_ID = "***************"; 25 26 /** 27 * 百度鷹眼接口URL 28 */ 29 public static final String BAIDU_YINGYAN_URL = "http://yingyan.baidu.com/api/v3/"; 30 31 /** 32 * 百度鷹眼請求方式(POST) 33 */ 34 public static final String BAIDU_YINGYAN_REQ_POST = "POST"; 35 36 /** 37 * 百度鷹眼請求方式(GET) 38 */ 39 public static final String BAIDU_YINGYAN_REQ_GET = "GET"; 40 41 42 /** 43 * ClassName: BaiduYingyanEntity 44 * @Description: 終端管理 45 * @author ljwang 46 * @date 2017年9月4日 47 */ 48 public static class BaiduYingyanEntity { 49 /** 50 * 創建entity,並賦屬性信息 51 */ 52 public static final String ADD = "entity/add"; 53 54 /** 55 * 刪除entity 56 */ 57 public static final String DELETE = "entity/delete"; 58 59 /** 60 * 更新entity屬性信息 61 */ 62 public static final String UPDATE = "entity/update"; 63 64 /** 65 * 檢索符合條件的entity,返回entity屬性信息和最新位置。可用於列出entity,也可用於批量查詢多個entitiy的位置 66 */ 67 public static final String LIST = "entity/list"; 68 } 69 70 71 /** 72 * @Description: 請求鷹眼 73 * @author ljwang 74 * @date 2017年9月4日 75 */ 76 public static String yingYanReq(String urlReq, String param, String method) { 77 try { 78 //Get請求,拼裝參數 79 if (BAIDU_YINGYAN_REQ_GET.equals(method)) { 80 urlReq = urlReq + "?" + param; 81 } 82 83 //創建URL對象 84 URL url = new URL(urlReq); 85 //返回一個URLConnection對象,它表示到URL所引用的遠程對象的連接 86 URLConnection urlConnection = url.openConnection(); 87 88 //POST請求,寫入參數 89 if (BAIDU_YINGYAN_REQ_POST.equals(method)) { 90 // 設置doOutput屬性為true表示將使用此urlConnection寫入數據 91 urlConnection.setDoOutput(true); 92 // 定義待寫入數據的內容類型,我們設置為application/x-www-form-urlencoded類型 93 urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 94 // 得到請求的輸出流對象 95 OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream()); 96 // 把數據寫入請求的Body 97 out.write(param); 98 out.flush(); 99 out.close(); 100 } 101 102 // 從服務器讀取響應 103 InputStream inputStream = urlConnection.getInputStream(); 104 String encoding = urlConnection.getContentEncoding(); 105 String result = IOUtils.toString(inputStream, encoding); 106 System.out.println(result); 107 return result; 108 } catch (IOException e) { 109 e.printStackTrace(); 110 return null; 111 } 112 } 113 114 public static void main(String[] args){ 115 116 /* 117 * 添加entity 118 */ 119 String urlReq = BAIDU_YINGYAN_URL + BaiduYingyanEntity.ADD; 120 String param = "ak=" + BAIDU_YINGYAN_AK_S + 121 "&service_id=" + BAIDU_YINGYAN_SERVICE_ID + 122 "&entity_name=" + "鷹眼測試名稱" + 123 "&entity_desc=" + "鷹眼測試描述"; 124 yingYanReq(urlReq, param, BAIDU_YINGYAN_REQ_POST); 125 126 /* 127 * 查詢entity 128 */ 129 // String urlReq = BAIDU_YINGYAN_URL + BaiduYingyanEntity.LIST; 130 // String param = "ak=" + BAIDU_YINGYAN_AK_S + 131 // "&service_id=" + BAIDU_YINGYAN_SERVICE_ID + 132 // "&filter=entity_names:鷹眼測試名稱"; 133 // yingYanReq(urlReq, param, BAIDU_YINGYAN_REQ_GET); 134 135 /* 136 * 修改entity 137 */ 138 // String urlReq = BAIDU_YINGYAN_URL + BaiduYingyanEntity.UPDATE; 139 // String param = "ak=" + BAIDU_YINGYAN_AK_S + 140 // "&service_id=" + BAIDU_YINGYAN_SERVICE_ID + 141 // "&entity_name=" + "鷹眼測試名稱" + 142 // "&entity_desc=" + "鷹眼修改測試"; 143 // yingYanReq(urlReq, param, BAIDU_YINGYAN_REQ_POST); 144 145 /* 146 * 刪除entity 147 */ 148 // String urlReq = BAIDU_YINGYAN_URL + BaiduYingyanEntity.DELETE; 149 // String param = "ak=" + BAIDU_YINGYAN_AK_S + 150 // "&service_id=" + BAIDU_YINGYAN_SERVICE_ID + 151 // "&entity_name=" + "鷹眼測試名稱"; 152 // yingYanReq(urlReq, param, BAIDU_YINGYAN_REQ_POST); 153 } 154 }