package com.curender.web.server.http; import java.io.IOException; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PutMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.curender.dal.util.ConstantsDal; /** * HttpClient GET POST PUT 请求 * @author huang * @date 2013-4-10 */ public class HttpRequest { protected static Log log = LogFactory.getLog(HttpRequest.class); private static HttpRequest httpRequst=null; private HttpRequest(){} public static HttpRequest getInstance(){ if(httpRequst==null){ synchronized(HttpRequest.class){ if(httpRequst == null){ httpRequst=new HttpRequest(); } } } return httpRequst; } /** * HttpClient GET请求 * @author huang * @date 2013-4-9 * @param uri * @return resStr 请求返回的JSON数据 */ public String doGet(String uri){ String resStr = null; HttpClient htpClient = new HttpClient(); GetMethod getMethod = new GetMethod(ConstantsDal.SERVER_URL+uri); getMethod.getParams().setParameter( HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try{ int statusCode = htpClient.executeMethod( getMethod ); // log.info(statusCode); if(statusCode != HttpStatus.SC_OK){ log.error("Method failed: "+getMethod.getStatusLine()); return resStr; } byte[] responseBody = getMethod.getResponseBody(); resStr = new String(responseBody,HttpConstants.ENCODED); } catch (HttpException e) { log.error("Please check your provided http address!"); //发生致命的异常,可能是协议不对或者返回的内容有问题 } catch (IOException e) { log.error( "Network anomaly" ); //发生网络异常 }finally{ getMethod.releaseConnection(); //释放连接 } return resStr; } /** * HttpClient POST请求 * @author huang * @date 2013-4-9 * @param s_user * @return resStr 请求返回的JSON数据 */ @SuppressWarnings( "deprecation" ) public String doPost(String uri,String jsonObj){ String resStr = null; HttpClient htpClient = new HttpClient(); PostMethod postMethod = new PostMethod(ConstantsDal.SERVER_URL+uri); postMethod.addRequestHeader( "Content-Type","application/json" ); postMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, HttpConstants.ENCODED ); postMethod.setRequestBody( jsonObj ); try{ int statusCode = htpClient.executeMethod( postMethod ); // log.info(statusCode); if(statusCode != HttpStatus.SC_OK){ //post和put不能自动处理转发 301:永久重定向,告诉客户端以后应从新地址访问 302:Moved Temporarily if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY||statusCode == HttpStatus.SC_MOVED_TEMPORARILY){ Header locationHeader = postMethod.getResponseHeader( "location" ); String location = null; if(locationHeader!=null){ location = locationHeader.getValue(); log.info("The page was redirected to :"+location); }else{ log.info("Location field value is null"); } }else{ log.error("Method failed: "+postMethod.getStatusLine()); } return resStr; } byte[] responseBody = postMethod.getResponseBody(); resStr = new String(responseBody,HttpConstants.ENCODED); }catch(Exception e){ e.printStackTrace(); }finally{ postMethod.releaseConnection(); } return resStr; } /** * HttpClient PUT请求 * @author huang * @date 2013-4-10 * @return */ @SuppressWarnings( "deprecation" ) public String doPut(String uri,String jsonObj){ String resStr = null; HttpClient htpClient = new HttpClient(); PutMethod putMethod = new PutMethod(ConstantsDal.SERVER_URL+uri); putMethod.addRequestHeader( "Content-Type","application/json" ); putMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, HttpConstants.ENCODED ); putMethod.setRequestBody( jsonObj ); try{ int statusCode = htpClient.executeMethod( putMethod ); // log.info(statusCode); if(statusCode != HttpStatus.SC_OK){ log.error("Method failed: "+putMethod.getStatusLine()); return null; } byte[] responseBody = putMethod.getResponseBody(); resStr = new String(responseBody,HttpConstants.ENCODED); }catch(Exception e){ e.printStackTrace(); }finally{ putMethod.releaseConnection(); } return resStr; } }
HttpConstants.ENCODED=“UTF-8”
ConstantsDal.SERVER_URL=http://ip/
相关推荐
HttpClient工具类,简单轻松的实现get,post,put和delete请求
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
这个代码的作用是使用HttpClient发送POST请求,...通过学习这个示例,你可以了解到如何使用HttpClient发送不同类型的HTTP请求(如GET、POST、PUT、DELETE等),以及如何处理响应结果(如读取响应体、解析JSON数据等)。
NULL 博文链接:https://jameskaron.iteye.com/blog/2302471
http接口通用调试工具,支持get、post、put、patch、delete方法,数据的发送和接收使用json格式,即Content-Type: application/json
http工具类,基于java,封装HttpClient的四种提交方式:post,get,delete,put
HttpClient接口调用工具类 (Post请求 get请求 put请求 delete请求 下载就可以用 )
HttpClient工具类封装,基于httpclient4.5.12,有get、post、put、delete方法,可设置连接超时时间,请求超时时间,socket读写超时时间,设置是否允许重定向,字符集
httpclient工具类包含get,post,put,delete请求
httpClient的get请求方式2 * @return * @throws Exception */ public static String doGet(String url, String charset) throws Exception { /* * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:...
httpclient-4.1.1.jar,httpclient,http get post put
HttpClient接口调用工具类 (Post请求 get请求 put请求 delete请求 (附带调用demo) 下载就可以用 ) 可设置httpclient的连接池大小,连接池最大并发连接数,单路由最大并发数设
http get put post, apache jar core 4.4.1
以下列出的是 HttpClient 提供的主要的功能,要知道更多详细的功能可以参见 HttpClient 的主页...(1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等) (2)支持自动转向 (3)支持 HTTPS 协议 (4)支持代理服务器等
2. 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。 3. 支持HTTPS协议。 4. 通过Http代理建立透明的连接。 5. 利用CONNECT方法通过Http代理建立隧道的https...
利用 android httpclient 编写http协议的例子代码,可以参考添加post,get ,put delete 等功能。工程师文件可以直接编译运行。
http远程请求接口的工具类HttpClientUtil.java,其中包含get,post,put,delete方式进行http远程请求。
实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等) 支持自动转向 支持 HTTPS 协议 支持代理服务器等 下面将逐一介绍怎样使用这些功能。首先,我们必须安装好 HttpClient。 HttpClient 可以在...