引用包
<!-- /artifact/commons-httpclient/commons-httpclient --><dependency><groupId>commons-httpclient</groupId><artifactId>commons-httpclient</artifactId><version>3.1</version></dependency>
package com.gfjh.core;import .InetAddress;import .SocketTimeoutException;import .UnknownHostException;import java.util.HashMap;import java.util.Map;import mons.httpclient.Header;import mons.httpclient.HttpClient;import mons.httpclient.HttpMethodBase;import mons.httpclient.NameValuePair;import mons.httpclient.cookie.CookiePolicy;import mons.httpclient.methods.DeleteMethod;import mons.httpclient.methods.GetMethod;import mons.httpclient.methods.HeadMethod;import mons.httpclient.methods.OptionsMethod;import mons.httpclient.methods.PostMethod;import mons.httpclient.methods.PutMethod;import mons.httpclient.methods.TraceMethod;import mons.httpclient.params.HttpMethodParams;import mons.lang3.StringUtils;public class c {public static void main(String[] args) {// TODO Auto-generated method stubString type = "Get";String url = "";Map<String, Object> map = new HashMap<String, Object>();double begin = System.currentTimeMillis();HttpMethodBase method = null;try {// 匹配请求if ("Post".equals(type)) {method = new PostMethod(url);} else if ("Get".equals(type)) {method = new GetMethod(url);} else if ("Delete".equals(type)) {method = new DeleteMethod(url);} else if ("Put".equals(type)) {method = new PutMethod(url);} else if ("Trace".equals(type)) {method = new TraceMethod(url);} else if ("Head".equals(type)) {method = new HeadMethod(url);} else if ("Options".equals(type)) {method = new OptionsMethod(url);}HttpClient client = new HttpClient();client.setConnectionTimeout(30000);client.setTimeout(30000);client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);method.setRequestHeader("Connection", "close");method.setRequestHeader("Referer", "");method.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/0101 Firefox/42.0");// 设置参数String[] keys = null, values = null;NameValuePair[] params = new NameValuePair[keys.length];for (int i = 0; i < keys.length; i++) {params[i] = new NameValuePair(keys[i], values[i]);}method.setQueryString(params);// 设置请求头 和上面方法一致,如果一样,那么就覆盖。String[] headKeys = null, headValues = null;if (headKeys.length > 0) {for (int i = 0; i < headKeys.length; i++) {method.setRequestHeader(headKeys[i], headValues[i]);}}int status = client.executeMethod(method);map.put("status", status);Header[] heads = method.getResponseHeaders();// 获取请求的Host,用于IP解析String host = method.getHostConfiguration().getHost();StringBuffer sb = new StringBuffer("");for (Header header : heads) {sb.append(header.getName()).append(" : ").append(header.getValue()).append("\r\n");}// 头信息map.put("head", sb.toString());String result = method.getResponseBodyAsString();map.put("result", result);String ip = "未知";try {InetAddress iAddress = InetAddress.getByName(host);ip = iAddress.getHostAddress();map.put("host", ip);} catch (Exception e) {System.out.println("获取IP地址失败,域名:" + host);}} catch (UnknownHostException e) {// 域名错误map.put("message", "请输入正确的网址");System.out.println("网址异常[%s]" + url);} catch (SocketTimeoutException e) {// 超时map.put("message", "请求地址超时");System.out.println("请求地址超时[%s]" + url);} catch (Exception e) {// 其他异常map.put("message", "请求出现未知异常,请重试!");System.out.println("请求出现未知异常,请重试!" + url);}double end = System.currentTimeMillis();map.put("time", (end - begin) / 1000);}}
如果觉得《HTTP接口测试代码 HTTP GET/POST模拟请求测试工具》对你有帮助,请点赞、收藏,并留下你的观点哦!