http post请求

http post请求
代码
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
/**
 * 
 * POST接口服务的JAVA示例
 * 
 */
public class HttpPostApiInvoker {
 
    private final static String URL = "http://test/url";					// 请求url
    private final static String REQUEST_CHARSET     =   "UTF-8";
    private final static String RESPONSE_CHARSET    =   "UTF-8";
    private final static int    CONNECT_TIMEOUT     =   5000;               // 调用连接超时时间
    private final static int    REQUEST_TIMEOUT     =   10000;              // 调用获取数据超时时间
 
    public static void main(String[] args) throws Exception{
    	HttpPostApiInvoker httpPostApiInvoker = new HttpPostApiInvoker();
        List<NameValuePair>  parameterList = httpPostApiInvoker.prepareParameter();
        String result = httpPostApiInvoker.invoke(parameterList);
        System.out.println(result);
    }
 
    /***
     * 准备业务参数
     * @return
     */
    private List<NameValuePair> prepareParameter(){
        List<NameValuePair> parameterList = new ArrayList<>();
        parameterList.add(new BasicNameValuePair("name", "xxxx"));
        parameterList.add(new BasicNameValuePair("password", "xxxxxxxxx"));
        return parameterList;
    }
 
    /***
     * 没有使用长连接
     * @param parameterList
     * @return  JSON格式的字符串
     * @throws IOException
     */
    private String invoke(List<NameValuePair> parameterList) throws IOException {
        HttpPost httpPost = null;
        String result = "";
        try {
 
            httpPost = new HttpPost(URL);
 
            //设置超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT)
                    .setSocketTimeout(REQUEST_TIMEOUT)
                    .build();
            httpPost.setConfig(requestConfig);
 
            CloseableHttpClient httpClient = HttpClients.createDefault();
            httpPost.setEntity(new UrlEncodedFormEntity(parameterList, REQUEST_CHARSET));
 
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
 
            //返回值不是200,进行错误处理
            if(HttpStatus.SC_OK != httpResponse.getStatusLine().getStatusCode()){
 
                //相关处理
                if(null != httpPost) {
                    httpPost.abort();
                }
                return result;
            }
 
            //获取结果
            result = EntityUtils.toString(httpEntity, RESPONSE_CHARSET);
 
        }catch(Exception e){
            if(null != httpPost){
                httpPost.abort();
            }
            throw e;
        }
        return result;
    }
}
项目依赖
<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-lang3</artifactId>
 <version>3.3.2 </version>
</dependency>

<dependency>
 <groupId>org.apache.httpcomponents</groupId>
 <artifactId>httpclient</artifactId>
 <version> 4.3.6</version>
</dependency>

<dependency>
 <groupId>org.apache.httpcomponents</groupId>
 <artifactId>httpcore</artifactId>
 <version>4.4</version>
</dependency>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值