import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class HttpUtil {
private static final Logger LOG = LoggerFactory.getLogger(HttpUtil.class);
public static DefaultHttpClient getHttpsClient()
{
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] chain,
String authType)
throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain,
String authType)
throws java.security.cert.CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
DefaultHttpClient client = new DefaultHttpClient();
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ClientConnectionManager ccm = client.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
//设置要使用的端口,默认是443
sr.register(new Scheme("https", 443, ssf));
return client;
} catch (Exception ex) {
LOG.error("", ex);
return null;
}
}
/**
*
* @param url
* @param params
* @return
* @author asflex
* @date 2014-3-28下午7:24:02
* @modify 2014-3-28下午7:24:02
*/
public static String post(String url, Map<String, String> params) {
DefaultHttpClient httpClient = getHttpsClient();
HttpPost post = new HttpPost(url);
HttpEntity entity = map2UrlEncodedFormEntity(params);
if(entity != null) {
post.setEntity(entity);
}
LOG.info("http post---{}", getUrlRequestInfo(url, params));
HttpResponse response;
try {
response = httpClient.execute(post);
String result = EntityUtils.toString(response.getEntity());
return result;
} catch (ClientProtocolException e) {
LOG.error("", e);
} catch (IOException e) {
LOG.error("", e);
} finally {
httpClient.getConnectionManager().shutdown();
}
return null;
}
/**
* 生成post请求时的记录
* @param url
* @param params
* @return
* @author asflex
* @date 2014-3-28下午7:23:33
* @modify 2014-3-28下午7:23:33
*/
public static String getUrlRequestInfo(String url, Map<String, String> params) {
StringBuilder paramStr = new StringBuilder();
if(MapUtils.isNotEmpty(params)) {
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
Joiner.on("&").appendTo(paramStr, iterator);
}
return String.format("curl -d '%s' '%s'", StringUtils.trimToEmpty(paramStr.toString()), StringUtils.trimToEmpty(url));
}
/**
* 参数转换
* @param params
* @return
* @author asflex
* @date 2014-3-28下午7:23:05
* @modify 2014-3-28下午7:23:05
*/
public static HttpEntity map2UrlEncodedFormEntity(Map<String, String> params) {
if(MapUtils.isNotEmpty(params)) {
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
List<NameValuePair> nvps = Lists.newArrayList();
while(iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
nvps.add(new BasicNameValuePair(StringUtils.trimToEmpty(entry.getKey()), StringUtils.trimToEmpty(entry.getValue())));
}
try {
return new UrlEncodedFormEntity(nvps);
} catch (UnsupportedEncodingException e) {
LOG.error("", e);
}
}
return null;
}
public static void main(String[] args) {
Map<String, String> params = Maps.newHashMapWithExpectedSize(8);
params.put("username", "fred5");
params.put("password", "1111");
params.put("vcode", "");
System.out.println(getUrlRequestInfo("", params));
System.out.println(post("https://www.baidu.com/webApi/logins.jsp", params));
}
}
分享到:
相关推荐
不会的可以在评论区留言哈,这是我自己做项目用到的。所以绝对可用!同时共享出来给到大家
通过HTTPClient发起HTTPS GET请求(返回值为字符串)、POST请求(返回值为JSONOBJECT),需引入fastJson的jar包
esp8266 发起https 请求,请求 https 的api 等等,整个项目工程
使用HttpClient4.5实现https请求忽略SSL证书验证工具类
(完整版)JAVA利用HttpClient进行POST请求(HTTPS).doc
使用 HTTP Client 请求 HTTPS 的 API 时出现 The certificate cannot be verified up to a trusted certification authority 异常,并且证书已经传入。 下面就是问题代码: public class Program { public static...
该工具类使用httpclient进行http or https请求,包括requestbody格式和form表单格式,另外含文件服务器中转上传方法,几乎支持所有常用接口调用,内含详细注释和说明文件,含jar包,及maven方式引用,拿过去直接用吧
本文主要介绍了请求HTPPS的实例.帮助请参考;http://blog.csdn.net/zhangxiaowei_/article/details/39339775
除此之外,我们对请求添加签名的校验,key设置为X-Sign 接口定义 /// /// 获取时间戳(毫秒). /// /// <param name=dateTime></param> /// <returns></returns> [HttpPost] [AllowAnonymous]
详细说明在https://blog.csdn.net/zyf2533/article/details/118294101?spm=1001.2014.3001.5501
解决证书错误,利用httpClient发送https请求,内容格式为XML,另外包中有sha1加密,需要的包,速速下载
以上自己测试是可行的,直接拿过去导入即可,直接用main
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
NULL 博文链接:https://javasam.iteye.com/blog/2117845
这是一个关于httpclient4.5访问https网站的例子,可以绕过ssl认证直接访问
httpclient发送post请求
下面小编就为大家带来一篇JAVA利用HttpClient进行POST请求(HTTPS)实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起 小编过来看看吧
NULL 博文链接:https://duanfei.iteye.com/blog/1944927
NULL 博文链接:https://daoshud1.iteye.com/blog/1847378
NULL 博文链接:https://bijian1013.iteye.com/blog/2310211