public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
HttpClient发送post请求的类
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/*
* 利用HttpClient进行post请求的工具类
*/
public class HttpClientUtil {
public String doPost(String url,MapString,String map,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
ListNameValuePair list = new ArrayListNameValuePair();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
EntryString,String elem = (EntryString, String) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size()0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
测试代码
import java.util.HashMap;
import java.util.Map;
//对接口进行测试
public class TestMain {
private String url = "";
private String charset = "utf-8";
private HttpClientUtil httpClientUtil = null;
public TestMain(){
httpClientUtil = new HttpClientUtil();
}
public void test(){
String httpOrgCreateTest = url + "httpOrg/create";
MapString,String createMap = new HashMapString,String();
createMap.put("authuser","*****");
createMap.put("authpass","*****");
createMap.put("orgkey","****");
createMap.put("orgname","****");
String httpOrgCreateTestRtn = httpClientUtil.doPost(httpOrgCreateTest,createMap,charset);
System.out.println("result:"+httpOrgCreateTestRtn);
}
public static void main(String[] args){
TestMain main = new TestMain();
main.test();
}
}
如何在java7中启用tlsv1.2TLS1.0目前对于某些浏览器或者浏览器的版本来说已经过期了 。可以通过对服务器的稍加修复,就可以实现TLS1.2了 。不过前提是有部署了SSL证书,如果还有什么关于TLS协议的问题,可以咨询一下沃通证书签发中心 。
tlsjava代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于throwsjava、tlsjava代码的信息别忘了在本站进行查找喔 。
推荐阅读
- ppt怎么转换为pdf,pdf如何转化为ppt
- cpu什么好些,cpu哪些比较好
- windows下redis启动服务器,redis启动服务端
- word你如何输入横线,word里如何输入横线
- vb.net文件下文件 vbnet fileopen
- c上传文件到服务器,c 文件传输
- p40微信没有视频号,华为p40微信里为什么没有视频号
- php组数新增数据库 php创建数据库
- yunos系统好还是鸿蒙好,yunos好用吗