导航:首页 > 编程语言 > java生成ssl

java生成ssl

发布时间:2023-03-08 01:42:22

1. 求解java怎样发送https请求

使用httpClient可以发送,具体的可以参考下面的代码

SSLClient类,继承至HttpClient

importjava.security.cert.CertificateException;
importjava.security.cert.X509Certificate;
importjavax.net.ssl.SSLContext;
importjavax.net.ssl.TrustManager;
importjavax.net.ssl.X509TrustManager;
importorg.apache.http.conn.ClientConnectionManager;
importorg.apache.http.conn.scheme.Scheme;
importorg.apache.http.conn.scheme.SchemeRegistry;
importorg.apache.http.conn.ssl.SSLSocketFactory;
importorg.apache.http.impl.client.DefaultHttpClient;
//用于进行Https请求的HttpClient
{
publicSSLClient()throwsException{
super();
SSLContextctx=SSLContext.getInstance("TLS");
X509TrustManagertm=newX509TrustManager(){
@Override
publicvoidcheckClientTrusted(X509Certificate[]chain,
StringauthType)throwsCertificateException{
}
@Override
publicvoidcheckServerTrusted(X509Certificate[]chain,
StringauthType)throwsCertificateException{
}
@Override
publicX509Certificate[]getAcceptedIssuers(){
returnnull;
}
};
ctx.init(null,newTrustManager[]{tm},null);
SSLSocketFactoryssf=newSSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManagerccm=this.getConnectionManager();
SchemeRegistrysr=ccm.getSchemeRegistry();
sr.register(newScheme("https",443,ssf));
}
}

HttpClient发送post请求的类

importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Map.Entry;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.NameValuePair;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.entity.UrlEncodedFormEntity;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.message.BasicNameValuePair;
importorg.apache.http.util.EntityUtils;
/*
*利用HttpClient进行post请求的工具类
*/
publicclassHttpClientUtil{
publicStringdoPost(Stringurl,Map<String,String>map,Stringcharset){
HttpClienthttpClient=null;
HttpPosthttpPost=null;
Stringresult=null;
try{
httpClient=newSSLClient();
httpPost=newHttpPost(url);
//设置参数
List<NameValuePair>list=newArrayList<NameValuePair>();
Iteratoriterator=map.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String>elem=(Entry<String,String>)iterator.next();
list.add(newBasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size()>0){
UrlEncodedFormEntityentity=newUrlEncodedFormEntity(list,charset);
httpPost.setEntity(entity);
}
HttpResponseresponse=httpClient.execute(httpPost);
if(response!=null){
HttpEntityresEntity=response.getEntity();
if(resEntity!=null){
result=EntityUtils.toString(resEntity,charset);
}
}
}catch(Exceptionex){
ex.printStackTrace();
}
returnresult;
}
}

测试代码

importjava.util.HashMap;
importjava.util.Map;
//对接口进行测试
publicclassTestMain{
privateStringurl="https://192.168.1.101/";
privateStringcharset="utf-8";
=null;

publicTestMain(){
httpClientUtil=newHttpClientUtil();
}

publicvoidtest(){
StringhttpOrgCreateTest=url+"httpOrg/create";
Map<String,String>createMap=newHashMap<String,String>();
createMap.put("authuser","*****");
createMap.put("authpass","*****");
createMap.put("orgkey","****");
createMap.put("orgname","****");
StringhttpOrgCreateTestRtn=httpClientUtil.doPost(httpOrgCreateTest,createMap,charset);
System.out.println("result:"+httpOrgCreateTestRtn);
}

publicstaticvoidmain(String[]args){
TestMainmain=newTestMain();
main.test();
}
}

2. java websocket 客户端怎么实现SSL认证或者进行不检证,大家帮帮忙,急

不知道具体需求是什么。如果用java
web作为服务端,那可以在web项目启动的时候,增加socket监听服务。在web项目的listener增加启动类。
或者干脆使用如netty这样的io框架。

阅读全文

与java生成ssl相关的资料

热点内容
android录音开源 浏览:453
弗洛伊德算法c 浏览:6
udp命令字 浏览:659
app服务端java源码 浏览:798
电脑用文件夹玩大型游戏 浏览:254
安卓耳塞失灵怎么办 浏览:765
华三交换机保存命令 浏览:605
命令方块怎么调键盘 浏览:841
不把密码存在服务器上怎么办 浏览:398
怎么让指令方块的命令消失 浏览:543
用单片机做plc 浏览:404
云服务器进入子目录命令 浏览:795
服务器机柜如何配电 浏览:578
怎么删除iphone资源库里的app 浏览:940
pdf鱼 浏览:648
单片机pcf8591什么作用 浏览:805
sql命令学院 浏览:283
加密软件在电脑那个盘 浏览:988
android获取外部存储 浏览:573
怎么查自己家的服务器地址 浏览:858