最近项目中用到了模拟HTTP请求的部分,下面做了一个小整合,希望能给大家提供一定的参考,如果写的有问题,请帮忙指正,谢谢。
/** * <设置请求头及请求属性> * <功能详细描述> * @param method 请求对象 * @param contentType 内容类型 * @see [类、类#方法、类#成员] */ private static void setMethodParams(HttpMethodBase method, String contentType) { // 连接类型 method.addRequestHeader("Connection", "keep_live"); // 内容类型 if (!isEmpty(contentType)) { method.addRequestHeader("Content-Type", contentType); } // 设置POST请求超时时间 method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, httpRequestTimeout); // 消息头里带上鉴权 method.addRequestHeader("Authorization", authorization); } /** * <使用POST方式提交HTTP请求> * <功能详细描述> * @param address 请求地址 * @param entity 请求参数 * @return 返回请求结果 * @throws HttpException * @throws IOException * @see [类、类#方法、类#成员] */ public static String sendPostRequest(String address, RequestEntity entity) throws HttpException, IOException { // 如果请求地址为空直接退出请求 logger.info("Request Address Is " + address); if (isEmpty(address)) { return ""; } // 存储服务器的响应消息 String responseMsg = ""; // 客户端对象 HttpClient client = new HttpClient(); // 设置连接超时时间 client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout); // POST对象 PostMethod method = new PostMethod(address); // 设置请求头及属性 setMethodParams(method, null); // 参数对象 if (null != entity) { method.setRequestEntity(entity); } // 执行的结果 client.executeMethod(method); responseMsg = method.getResponseBodyAsString(); logger.info("Response Message Is " + responseMsg); // 释放客户端连接 method.releaseConnection(); // 返回响应结果 return responseMsg; } /** * <使用POST方式提交HTTP请求> * <包含多个请求参数时使用该方法> * @param address 请求地址 * @param parts 多个请求参数集合 * @return 返回请求结果 * @throws HttpException * @throws IOException * @see [类、类#方法、类#成员] */ public static String sendPostRequest(String address, NameValuePair[] values) throws HttpException, IOException { // 如果请求地址为空直接退出请求 logger.info("Request Address Is " + address); if (isEmpty(address)) { return ""; } // 存储服务器的响应消息 String responseMsg = ""; // 客户端对象 HttpClient client = new HttpClient(); // 设置连接超时时间 client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout); // POST对象 PostMethod method = new PostMethod(address); // 设置请求头及属性 setMethodParams(method, "application/x-www-form-urlencoded"); // 参数对象 if (null != values) { method.setRequestBody(values); } // 执行的结果 client.executeMethod(method); responseMsg = method.getResponseBodyAsString(); logger.info("Response Message Is " + responseMsg); // 释放客户端连接 method.releaseConnection(); // 返回响应结果 return responseMsg; } /** * <使用POST方式提交HTTP请求> * <多个请求参数包含文件时使用该方法> * @param address 请求地址 * @param parts 多个请求参数集合 * @return 返回请求结果 * @throws HttpException * @throws IOException * @see [类、类#方法、类#成员] */ public static String sendPostRequest(String address, Part[] parts) throws HttpException, IOException { // 如果请求地址为空直接退出请求 logger.info("Request Address Is " + address); if (isEmpty(address)) { return ""; } // 存储服务器的响应消息 String responseMsg = ""; // 客户端对象 HttpClient client = new HttpClient(); // 设置连接超时时间 client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout); // POST对象 PostMethod method = new PostMethod(address); // 设置请求头及属性 setMethodParams(method, null); // 参数对象 if (null != parts) { method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); } // 执行的结果 client.executeMethod(method); responseMsg = method.getResponseBodyAsString(); logger.info("Response Message Is " + responseMsg); // 释放客户端连接 method.releaseConnection(); // 返回响应结果 return responseMsg; } /** * <使用POST方式提交HTTP请求> * <包含多个请求参数时使用该方法> * @param address 请求地址 * @param params 参数字符串 * @return 返回请求结果 * @throws HttpException * @throws IOException * @see [类、类#方法、类#成员] */ public static String sendPostRequest(String address, String params) throws HttpException, IOException { // 如果请求地址为空直接退出请求 logger.info("Request Address Is " + address); if (isEmpty(address)) { return ""; } // 参数对象 if (null != params) { address = address + "?" + params; } // 存储服务器的响应消息 String responseMsg = ""; // 客户端对象 HttpClient client = new HttpClient(); // 设置连接超时时间 client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout); // POST对象 PostMethod method = new PostMethod(address); // 设置请求头及属性 setMethodParams(method, "application/x-www-form-urlencoded"); // 执行的结果 client.executeMethod(method); responseMsg = method.getResponseBodyAsString(); logger.info("Response Message Is " + responseMsg); // 释放客户端连接 method.releaseConnection(); // 返回响应结果 return responseMsg; } /** * <使用GET方式提交HTTP请求> * <功能详细描述> * @param address 请求地址 * @param params 请求参数 * @return 返回请求结果 * @throws HttpException * @throws IOException * @see [类、类#方法、类#成员] */ public static String sendGetRequest(String address, String params) throws HttpException, IOException { // 如果请求地址为空直接退出请求 logger.info("Request Address Is " + address); if (isEmpty(address)) { return ""; } // 存储服务器的响应消息 String responseMsg = ""; // 客户端对象 HttpClient client = new HttpClient(); // 设置连接超时时间 client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout); // GET对象 GetMethod method = null; if (!isEmpty(params)) { method = new GetMethod(address + "?" + params); } else { method = new GetMethod(address); } // 设置请求头及属性 setMethodParams(method, null); // 执行的结果 client.executeMethod(method); responseMsg = method.getResponseBodyAsString(); logger.info("Response Message Is " + responseMsg); // 释放客户端连接 method.releaseConnection(); // 返回响应结果 return responseMsg; } /** * <使用GET方式提交HTTP请求> * <功能详细描述> * @param address 请求地址 * @param params 请求参数 * @return 返回请求结果 * @throws HttpException * @throws IOException * @see [类、类#方法、类#成员] */ public static String[] sendGetRequest(String[] address, String params) throws HttpException, IOException { // 如果请求地址为空直接退出请求 logger.info("Request Address Is " + address); if (null == address) { return null; } // 存储响应结果 String[] responseMsg = new String[address.length]; // 客户端对象 HttpClient client = new HttpClient(); // 设置连接超时时间 client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout); // GET对象 GetMethod method = null; for (int i = 0; i < address.length; i++) { if (!isEmpty(params)) { method = new GetMethod(address[i] + "?" + params); } else { method = new GetMethod(address[i]); } // 设置请求头及属性 setMethodParams(method, null); // 执行的结果 client.executeMethod(method); String result = method.getResponseBodyAsString(); responseMsg[i] = result; logger.info("Response Message Is " + result); } // 释放客户端连接 method.releaseConnection(); // 返回响应结果 return responseMsg; }
注:authorization字符串的计算方式如下:
/** * <获取xml文件中的值赋给全局变量> * <功能详细描述> * @see [类、类#方法、类#成员] */ public static void readConfigXml() { try { // 获取资源流 InputStream in = Global.class.getResourceAsStream("/config.xml"); // 获取根节点 SAXReader sax = new SAXReader(); sax.setEncoding("UTF-8"); Document doc = sax.read(in); Element root = doc.getRootElement(); // 路由器用户名 apName = root.elementText("apName"); // 路由器密码 apPwd = root.elementText("apPwd"); // 链接超时时间 httpConnectionTimeout = Integer.parseInt(root.elementText("httpConnectionTimeout")) * 1000; // 请求超时时间 httpRequestTimeout = Integer.parseInt(root.elementText("httpRequestTimeout")) * 1000; // 检查次数 checkCount = Integer.parseInt(root.elementText("checkCount")); // 鉴权字符串 authorization = "Basic " + DatatypeConverter.printBase64Binary((apName + ":" + apPwd).getBytes("UTF-8")); } catch (Exception e) { logger.error(LOG_EXCEPTION_NAME, e); } }
相关推荐
简单使用方法: public static void main(String[] args) { // String str1 = "http://dev.d-smart.cn/Login"; // http协议路径 String str1 = ""; HttpClient httpClient = new ...
【ASP.NET编程知识】.NET CORE HttpClient的使用方法.docx
最近调试android接口,封装了几个 HttpClient通用方法
httpclient的介绍以及基本方法整理
httpclient是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,本文档提供使用httpclient的使用方法
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。 1. 创建HttpClient对象。 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建...
代码有非常详细地注释,下载java相关包可以直接运行,希望能为将要和打算要学习HtmlClient的人提供一些前期的帮助。
本文首先介绍 HTTPClient,然后根据作者实际工作经验给出了一些常见问题的解决方法。HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。...
Android HttpClient基本使用方法 测试过能用。
完整的上传实例包括图片和参数配置,可以通过该样例进行扩展使用
赠送jar包:httpclient-4.5.5.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
赠送jar包:httpclient-4.5.6.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。