1. HTTP GET请求
/**
* 向指定URL发送GET方法的请求
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String httpGet(String reqUrl, Map<String, String> headers){
return httpRequest(reqUrl, headers);
}
public static String httpRequest(String reqUrl, Map<String, String> otherHeaders){
URL url;
try {
url = new URL(reqUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String userName = "admin";
String password = "nsfocus";
String auth = userName + ":" + password;
BASE64Encoder enc = new BASE64Encoder();
String encoding = enc.encode(auth.getBytes());
conn.setDoOutput(true);
conn.setRequestProperty("Authorization", "Basic " + encoding);
for(Entry<String, String> entry : otherHeaders.entrySet()){
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
connection.connect();
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(),"utf-8"));
StringBuilder sb = new StringBuilder();
String line="";
while ((line = reader.readLine()) != null){
sb.append(line);
}
reader.close();
connection.disconnect();
return sb.toString();
String result = sb.toString();
root = mapper.readTree(result);
Iterator<JsonNode> rootNode = root.elements();
while(rootNode.hasNext()){
JsonNode c = rootNode.next();
JsonNode sites = c.path("site");
String ids = sites.path("id").toString();
id = ids.substring(1, ids.length()-1);
}
} catch (IOException e) {
log.error("error while getting request: {}", e.getMessage());
return id;
}
}
2. HTTP POST请求
/**
* 向指定URL发送POST方法的请求
* @param url 发送请求的URL
* @return URL 所代表远程资源的响应结果
*/
public static String httpPost(String reqUrl, String content, Map<String, String> headers){
return httpRequest(reqUrl, "POST", content, headers);
}
public static String httpRequest(String reqUrl, String method, String content,
Map<String, String> headers){
URL url;
try {
url = new URL(reqUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(method);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
for(Entry<String, String> entry : headers.entrySet()){
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(),"utf-8"));
StringBuilder sb = new StringBuilder();
String line="";
while ((line = reader.readLine()) != null){
sb.append(line);
}
reader.close();
connection.disconnect();
return sb.toString();
} catch (IOException e) {
log.error("error while posting request: {}", e.getMessage());
return null;
}
}
3. HTTP PUT请求
public void httpPut(String dst_ip) {
GlobalConfig global = GlobalConfig.getInstance();
String urlStr = global.sncHost + "/devices/1/acl/aclGroups/aclGroup";
String xmlInfo = createXmlInfo(dst_ip);
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("PUT");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(xmlInfo.getBytes("utf-8"));
os.flush();
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
String result = "";
while( (line =br.readLine()) != null ){
result += line;
}
log.info(result);
br.close();
} catch (Exception e) {
log.info("Error in pushing policy now");
e.printStackTrace();
}
}