概述
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
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.BasicCredentialsProvider;
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;
import org.junit.Test;
public class HttpClientUtil {
/**
* 采用代理方式post提交
* @return
*/
public static String GetProxyPostResult(String proxyHost,int proxyPort,String userName,String password,String postUrl,Map<String,String> parameters){
CloseableHttpClient httpclient = null;
if(!StringUtils.isBlank(userName)){
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(userName, password));
httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
}
else
HttpClients.createDefault();
HttpHost proxy = null;
if(proxyPort != 0 && proxyPort !=80){
proxy = new HttpHost(proxyHost, proxyPort);
}
else
proxy = new HttpHost(proxyHost);
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpPost post = new HttpPost(postUrl);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if(null != parameters && parameters.size()>0){
for(Map.Entry<String,String> entry : parameters.entrySet()){
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
try {
post.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
post.setConfig(config);
CloseableHttpResponse response = httpclient.execute(post);
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
return null;
}
}
/**
* 采用代理方式post提交
* 只有代理服务器和端口,不需账号密码验证
* @return
*/
public static String GetProxyPostResult(String proxyHost,int proxyPort,String postUrl,Map<String,String> parameters){
return GetProxyPostResult(proxyHost,proxyPort,null,null,postUrl,parameters);
}
/**
* 采用代理方式post提交
* 只有代理服务器
* @return
*/
public static String GetProxyPostResult(String proxyHost,String postUrl,Map<String,String> parameters){
return GetProxyPostResult(proxyHost,0,null,null,postUrl,parameters);
}
/**
* 直接post提交,无需代理
*/
public static String GetDefaultPostResult(String postUrl,Map<String,String> parameters){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post = new HttpPost(postUrl);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if(null != parameters && parameters.size()>0){
for(Map.Entry<String,String> entry : parameters.entrySet()){
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
try {
post.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
CloseableHttpResponse response = httpclient.execute(post);
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
return null;
}
}
}
最后
以上就是机智镜子为你收集整理的httpclient提交实例代理方式(账号密码)非代理方式的全部内容,希望文章能够帮你解决httpclient提交实例代理方式(账号密码)非代理方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复