httpClient4.3 模拟Post提交,模拟Post上传文件并解决服务端使用spingMVC时upload.parseRequest(request)解析不到File,Pos简单抓取页面数据

该博客介绍了如何使用httpClient4.3进行模拟POST提交,包括上传文件,并针对服务端使用SpringMVC时可能出现的FileItem解析问题提供了解决方案。同时展示了设置请求配置、禁止重定向以及处理响应状态码来获取页面数据的方法。
摘要由CSDN通过智能技术生成


      import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
httpclient连接池Utils



import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
httpclient连接池Utils

public class HttpClientUtils {
    
    /**httpclient连接池Utils
     * produce http client
     *
     * @return
     * @throws KeyStoreException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    public static CloseableHttpClient acceptsHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        HttpClientBuilder b = HttpClientBuilder.create();
        
        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager();
        connMgr.setMaxTotal(200);
        connMgr.setDefaultMaxPerRoute(100);
        b.setConnectionManager(connMgr);
        
        CloseableHttpClient client = b.build();
        
        return client;
    }
    
}



   


代码片段模拟Post提交方式

CloseableHttpClient httpclient = HttpClientUitls.acceptsHttpClient();

        String JSONstr = null;//求情返回的JSON数据
        UrlEncodedFormEntity uefEntity;
        CloseableHttpResponse closeableHttpResponse=null;
  
        String url="url路径";
        List<NameValuePair> formparams=null;
        
            try {
                HttpPost httppost = new HttpPost(url);
                
				if (formparams == null) {//提交的参数
                    formparams = new ArrayList<NameValuePair>();
                }
                
                //具体需求具体参数
               // formparams.add(new BasicNameValuePair("pageNumber", pageNumber.toString()));
                //formparams.add(new BasicNameValuePair("pageSize", pageSize.toString()));
                
                
                uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
                httppost.setEntity(uefEntity);
                closeableHttpResponse = httpclient.execute(httppost);
                
                
                
                HttpEntity entity = closeableHttpResponse.getEntity();
                if (entity != null) {
            
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值