httpclient
EntityEnclosingMethod httpMethod = new PostMethod(url);
// 此处运用连接池技术。
MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
// 设定参数:与每个主机的最大连接数
manager.getParams().setDefaultMaxConnectionsPerHost(10);
// 设定参数:客户端的总连接数
manager.getParams().setMaxTotalConnections(20);
// 使用连接池技术创建HttpClient对象
HttpClient httpClient = new HttpClient(manager);
// 设置超时时间
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
//设置请求头
httpMethod.setRequestHeader(type, value);
// 发送含xml消息体
RequestEntity entity = new StringRequestEntity(xml, "text/xml", "UTF-8");
httpMethod.setRequestEntity(entity);
int resultCode = httpClient.executeMethod(httpMethod);
//获取响应消息头
Header[] headers = httpMethod.getResponseHeaders();
//返回响应报文
String xml = httpMethod.getResponseBodyAsString();
https
// 重写X509TrustManager类的三个方法,信任服务器证书
private X509TrustManager xtm = new X509TrustManager()
{
public void checkClientTrusted(X509Certificate[] chain, String authType)
{
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
{
}
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
};
// 信任主机
private HostnameVerifier hnv = new HostnameVerifier()
{
public boolean verify(String hostname, SSLSession session)
{
return true;
}
};
//构造SSLContext
/**
* 安全套接字的实现类
*/
SSLContext sslContext = null;
try
{
// sslContext = SSLContext.getInstance("TLS");
sslContext = SSLContext.getInstance("SSL");
X509TrustManager[] xtmArray = new X509TrustManager[] {xtm};
sslContext.init(null, xtmArray, new java.security.SecureRandom());
}
catch (GeneralSecurityException gse)
{
System.out.println("Https:DataSender:General Security Exception .");
gse.getStackTrace();
}
if (sslContext != null)
{
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
//通过Https发送消息
// System.setProperty("javax.net.debug","true");
URL remoteHostURL;
HttpsURLConnection httpsUrlConnection = null;
// 建立连接
try
{
remoteHostURL = new URL(ct.getRemoteHostURI());
System.out.println(ct.getRemoteHostURI());
httpsUrlConnection = (HttpsURLConnection)remoteHostURL.openConnection();
}
catch (Exception _ex)
{
System.out.println("Unable to openConnection on URL.");
_ex.printStackTrace();
}
byte[] Msg = ct.getMessage();
HttpsURLConnection.setFollowRedirects(true);
httpsUrlConnection.setInstanceFollowRedirects(true);
httpsUrlConnection.setDoOutput(true);
httpsUrlConnection.setDoInput(true);
httpsUrlConnection.setRequestMethod("POST");
httpsUrlConnection.setRequestProperty("Content-length", Integer.toString(Msg.length));
httpsUrlConnection.setRequestProperty("Content-type", "application/x-xxxxxxxx; version=1.0");
httpsUrlConnection.connect();
//发送消息体
OutputStream outStream = null;
try
{
outStream = httpsUrlConnection.getOutputStream();
outStream.write(Msg);
outStream.flush();
}
finally
{
if (outStream != null)
{
outStream.close();
}
}
//接收响应报文
int responseCode = httpsUrlConnection.getResponseCode();
InputStream is = httpsUrlConnection.getInputStream();
byte[] b = new byte[is.available()];
is.read(b);
System.out.println(new String(b));
分享到:
相关推荐
java 调用https发布的webservice接口实例及axis包及详细说明
Java抓取https网页数据,解决peer not authenticated异常。导入eclipse就能运行,带有所用的jar包(commons-httpclient-3.1.jar,commons-logging.jar,httpclient-4.2.5.jar,httpcore-4.2.4.jar)
Java发 Https请求工具类 支持SSL。 自定义请求方式 GET POST... 微信自用, 发Https请求工具类,支持SSL SSL Https
java访问https网址下载文件,当网页需要下载证书的时候,代码和操作步骤齐全。
java 发送https get请求工具类
压缩包里包含http和https两种请求,第一个文件是http请求包含post和get两种方式,剩下的是https的请求,最后一个文件里包含https的post和get两种方式。中间的两个是post 和 get 分开写的,按照需要引用吧
Java实现的Https请求工具类。 https的java工具类 Https
java发送https请求,解决了数组证书的问题,所依赖的jar都有
java用于模拟https需要的jar包 包含commons-lang-2.6.jar、httpclient-4.3.2.jar、httpcore-4.3.jar、commons-io-2.4.jar、httpmime-4.3.1.jar、httpclient-cache-4.3.1.jar
我使用jdk1.7,新建的是java项目,注意java compiler也是1.7,你需要把jar包拿过去,类拿过去
java发送https请求的例子
Java Https请求,跳过证书,直接访问 相关jar包全有 直接下载就能调试
java发送http/https请求(get/post)代码,java 通过发送json,post请求,返回json数据的方法 java 通过发送json,post请求,返回json数据的方法
java支持https的Post请求,采用SSLContext绕过https的安全证书校验,含实例main函数
windows系统上的http和https抓包工具, fiddler是http https监控工具 如果要监控java程序,需要添加如下代码: System.setProperty("http.proxyHost", "127.0.0.1"); System.setProperty("http.proxyPort", "8888...
java http 发送xml报文java http 发送xml报文java http 发送xml报文java http 发送xml报文java http 发送xml报文java http 发送xml报文java http 发送xml报文java http 发送xml报文java http 发送xml报文java http ...
NULL 博文链接:https://fanshuyao.iteye.com/blog/2436277
http客户端java程序,代码质量高,经过测试。
java生成https安全证书,解决httpClient访问https出错 编译:javac InstallCert.java 运行:java InstallCert 要访问的网址 结果:Enter certificate to add to trusted keystore or 'q' to quit: [1] 输入1确认生成...