java對第三方數據接口進行加密,具體方法如下:
package com.c;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public static void interfaceUtil(String path,String data) {
try {
URL url = new URL(path);
//打開和url之間的連接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
PrintWriter out = null;
//設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
//獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
//發(fā)送請求參數即數據
out.print(data);
//緩沖數據
out.flush();
//獲取URLConnection對象對應的輸入流
InputStream is = conn.getInputStream();
//構造一個字符流緩存
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null) {
System.out.println(str);
}
//關閉流
is.close();
//斷開連接
conn.disconnect();
System.out.println("完整結束");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
interfaceUtil("http://api.map.baidu.com/telematics/v3/weather?location=嘉興&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ", "");
}
}