HttpClient的简单用法
package com.dc.framework.util;import java.io.IOException;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import com.dc.brisk.soa.util.DataWindow;import com.dc.framework.component.ConfigResource;/** * 读取HTTP内容 */public class HttpClientUtil { private static final String appName = "physicalService"; private static final String backup_service = "snapshot_physical"; private static final String restore_service = "restore_physical"; private static final String ip; private static final int port; private static final String server_protocol; private static final String uri; static{ ip = ConfigResource.getConfig(ConfigResource.SERVER_IP); port = Integer.valueOf(ConfigResource.getConfig(ConfigResource.SERVER_PORT)); server_protocol = ConfigResource.getConfig(ConfigResource.SERVER_PROTOCOL); uri = ConfigResource.getConfig(ConfigResource.SERVER_URI); } public static void main(String[] args) throws HttpException, IOException { getRemoteHostStatus("10.1.180.178") ; } public static DataWindow getRemoteHostStatus(String ip){ DataWindow result = new DataWindow(DataWindow._NAME_VALUE); HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(ip,port,server_protocol); NameValuePair[] values = new NameValuePair[2]; values[0] = new NameValuePair("AppName","EquipMon"); values[1] = new NameValuePair("Service","Usage"); HttpMethod method = getPostMethod(uri,values); try { client.executeMethod(method); //打印服务器返回的状态 String response = new String(method.getResponseBodyAsString().getBytes("8859_1")); method.releaseConnection(); //打印返回的信息 result.putAttribute("status", method.getStatusCode()); /* * {"CPU":"16.0" ,"MEM":"52.310001373291016" ,"DISK0":"58.76" ,"DISK1":"5.08" ,"DISK2":"8.05" } */ String[] array = response.replace("{", "").replace("}", "").split(","); for(int i=0;i<array.length;i++) { String each = array[i]; String[] key_value = each.split(":"); key_value[0] = key_value[0].replaceAll(""", ""); key_value[1] = key_value[1].replaceAll(""", "").replaceAll("[\r\n]", ""); if("MEM".equals(key_value[0].trim())) { key_value[1] = key_value[1].substring(0,key_value[1].indexOf(".")+3); } result.putAttribute(key_value[0], key_value[1]); } } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } public static void printDataWindow(DataWindow data) { StringBuffer sb = new StringBuffer(); TransforTools.enCodeJson(data, sb); System.out.println(sb); } public static String execBackup(String backName,String src_volume_list,String target_volume_list) throws HttpException, IOException{ HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(ip,port,server_protocol); //使用 POST 方式提交数据 NameValuePair[] values = new NameValuePair[5]; values[0] = new NameValuePair("AppName",appName); values[1] = new NameValuePair("Service",backup_service); values[2] = new NameValuePair("backName",backName); values[3] = new NameValuePair("src_volume_list",src_volume_list); values[4] = new NameValuePair("target_volume_list",target_volume_list); HttpMethod method = getPostMethod(uri,values); client.executeMethod(method); //打印服务器返回的状态 System.out.println(method.getStatusLine()); // 打印结果页面 String response = new String(method.getResponseBodyAsString().getBytes("8859_1")); method.releaseConnection(); //打印返回的信息 System.out.println(response); return response; } public static String execRestore(String backName,String src_volume_list,String target_volume_list) throws HttpException, IOException{ HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(ip,port,server_protocol); //使用 POST 方式提交数据 NameValuePair[] values = new NameValuePair[5]; values[0] = new NameValuePair("AppName",appName); values[1] = new NameValuePair("Service",restore_service); values[2] = new NameValuePair("backName",backName); values[3] = new NameValuePair("src_volume_list",src_volume_list); values[4] = new NameValuePair("target_volume_list",target_volume_list); HttpMethod method = getPostMethod(uri,values); client.executeMethod(method); //打印服务器返回的状态 System.out.println(method.getStatusLine()); // 打印结果页面 String response = new String(method.getResponseBodyAsString().getBytes("8859_1")); method.releaseConnection(); //打印返回的信息 System.out.println(response); return response; } //使用 GET 方式提交数据 public static HttpMethod getGetMethod() { return new GetMethod("/simcard.php?simcard=1330227"); } //使用 POST 方式提交数据 public static HttpMethod getPostMethod(String uri,NameValuePair[] parametersBody) { PostMethod post = new PostMethod(uri); post.setRequestBody(parametersBody); return post; }}?