1. 常用依赖
< ! --httpClient Start--> < ! --httpClient 文件上传 Start--> < ! -- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> < dependency> < groupId> commons-fileupload< /groupId> < artifactId> commons-fileupload< /artifactId> < version> 1 .4 < /version> < /dependency> < ! -- https://mvnrepository.com/artifact/commons-io/commons-io --> < dependency> < groupId> commons-io< /groupId> < artifactId> commons-io< /artifactId> < version> 2.11 .0 < /version> < /dependency> < ! --httpClient 文件上传 End--> < ! -- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> < dependency> < groupId> org.apache.httpcomponents< /groupId> < artifactId> httpcore< /artifactId> < version> 4.4 .14 < /version> < /dependency> < ! -- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> < dependency> < groupId> commons-httpclient< /groupId> < artifactId> commons-httpclient< /artifactId> < version> 3 .1 < /version> < /dependency> < ! -- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime --> < dependency> < groupId> org.apache.httpcomponents< /groupId> < artifactId> httpmime< /artifactId> < version> 4.5 .13 < /version> < /dependency> < ! --httpClient End-->
2. 工具类
package com.gblfy.utils; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodRetryHandler;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams; import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration; /*** Http协议post请求** @author gblfy* @version 1.0 * @date 2020 -09-28*/
public class HttpApiUtil { /*** 协议类型:HTTP 请求方式:POST 报文格式:json 编码设置:UTF-8 响应类型:jsonStr** @param url 请求地址* @param json 请求报文* @param timeOut 超时时间* @return* @throws Exception*/public static String postJosnContent( String url, String json, int timeOut) throws Exception { System.out.println( "请求接口参数:" + json) ; HttpClient httpClient = new HttpClient( ) ; httpClient.getHttpConnectionManager( ) .getParams( ) .setSoTimeout( timeOut) ; PostMethod method = new PostMethod( url) ; method.getParams( ) .setParameter( HttpMethodParams.RETRY_HANDLER, ( HttpMethodRetryHandler) ( method1, exception, executionCount) -> false ) ; try { RequestEntity entity = new StringRequestEntity( json, "application/json" , "UTF-8" ) ; method.setRequestEntity( entity) ; httpClient.executeMethod( method) ; System.out.println( "请求接口路径url:" + method.getURI( ) .toString( )) ; InputStream in = method.getResponseBodyAsStream( ) ; // 下面将stream转换为StringStringBuffer sb = new StringBuffer( ) ; InputStreamReader isr = new InputStreamReader( in, "UTF-8" ) ; char[ ] b = new char[ 4096 ] ; for ( int n; ( n = isr.read( b)) != -1; ) { sb.append( new String( b, 0 , n)) ; } String returnStr = sb.toString( ) ; return returnStr; } catch ( Exception e) { e.printStackTrace( ) ; throw e; } finally { method.releaseConnection( ) ; } } public static String postJosnContent2( HttpServletRequest request, String url, String json, int timeOut) throws Exception { System.out.println( "请求接口参数:" + json) ; HttpClient httpClient = new HttpClient( ) ; httpClient.getHttpConnectionManager( ) .getParams( ) .setSoTimeout( timeOut) ; PostMethod method = new PostMethod( url) ; method.getParams( ) .setParameter( HttpMethodParams.RETRY_HANDLER, ( HttpMethodRetryHandler) ( method1, exception, executionCount) -> false ) ; try { RequestEntity entity = new StringRequestEntity( json, "application/json" , "UTF-8" ) ; method.setRequestEntity( entity) ; // 如果存在多个header的建议参数传递map,编译map添加header,下面二种添加header的场景情况//TODO 1 添加自定义header (不需要特殊处理)method.addRequestHeader( "自定义header key" , "自定义header value" ) ; //TODO 2 添加header 需要特殊处理for ( Enumeration< String> e = request.getHeaderNames( ) ; e.hasMoreElements( ) ; ) { String name = e.nextElement( ) .toString( ) ; System.out.println( name + ":" + request.getHeader( name)) ; if ( "Content-Type" .equalsIgnoreCase( name)) { method.addRequestHeader( name, request.getHeader( name)) ; } if ( "appid" .equalsIgnoreCase( name)) { method.addRequestHeader( name, request.getHeader( name)) ; } if ( "appsecret" .equalsIgnoreCase( name)) { method.addRequestHeader( name, request.getHeader( name)) ; } } httpClient.executeMethod( method) ; System.out.println( "请求接口路径url:" + method.getURI( ) .toString( )) ; InputStream in = method.getResponseBodyAsStream( ) ; // 下面将stream转换为StringStringBuffer sb = new StringBuffer( ) ; InputStreamReader isr = new InputStreamReader( in, "UTF-8" ) ; char[ ] b = new char[ 4096 ] ; for ( int n; ( n = isr.read( b)) != -1; ) { sb.append( new String( b, 0 , n)) ; } String returnStr = sb.toString( ) ; return returnStr; } catch ( Exception e) { e.printStackTrace( ) ; throw e; } finally { method.releaseConnection( ) ; } } // -------------------------------------------单元测试---------------------------------------
// public static void main( String[ ] args) throws Exception {
//
// // 客户端发送String url = "http://127.0.0.1:8080postToJson" ; String json = "{\" name\" :\" gblfy\" }" ;
// String res = postJosnContent( url, json,5000) ;
// System.out.println( "响应报文:" + res) ;
//
// // 服务端接收
// // @RequestMapping( value = "/postToJson" , method = RequestMethod.POST,produces =
// // "application/json;charset=UTF-8" )
// // @ResponseBody
// // public String postToJson( @RequestBody String json) {
// // System.out.println( json) ;
// // return json;
// } }