java发送https的请求
2015-12-29 14:54
561 查看
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.security.cert.CertificateException; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.security.cert.X509Certificate; public class HttpsGetData { private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } private static class TrustAnyHostnameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { return true; } } public void HttpsGet(){ try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom()); URL url = new URL( "https://www.xxxx.cn/api/check/" + URLEncoder.encode(realName, "utf-8") + "/" + cardNo);//此处中文乱码 HttpsURLConnection connection = (HttpsURLConnection) url .openConnection(); //设置https相关属性 connection.setSSLSocketFactory(sc.getSocketFactory()); connection.setHostnameVerifier(new TrustAnyHostnameVerifier()); connection.setDoOutput(true); //connection.setDoInput(true); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Accept-Language", "zh-CN"); connection.setRequestProperty("Accept-Charset", "UTF-8"); connection.setRequestProperty("contentType", "UTF-8"); connection .setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.setRequestMethod("GET"); connection.connect(); // 定义 BufferedReader输入流来读取URL的响应 BufferedReader in = new BufferedReader(new InputStreamReader( connection.getInputStream(), "UTF-8")); String line; String result = ""; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e1) { e1.printStackTrace(); } } } }
参考:
http://www.open-open.com/code/view/1432630203379 http://evan0625.iteye.com/blog/2064992
相关文章推荐
- Android 网络请求详解
- Android 网络请求详解
- CDN技术原理
- Neutron与Nova的网络逻辑关系---Neutron节点网络逻辑关系(二)
- 配置使用连接池的httpClient
- socket 连接的建立
- HTTP 错误 401.3 - Unauthorized asp.net mvc 图片,css,js没有权限访问
- iOS安全系列之一:HTTPS (轉載)
- codeforcese 498C. Array and Operations 网络流
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- Web Service 使用时出现 HTTP Status 401: Unauthorized
- java使用http创建https连接,并且使用http实现webservice服务端
- 三种方法实现移动端HTTPS加速和省电
- Java Socket网络编程常见异常(转)
- tomcat部署成https协议
- 【常用工具类】NetUtil(检测当前网络状态)
- curl HTTP 测试常用参数总结
- Objective-c语言_计算机网络(UI)同步get,post和异步get,post
- windows 7 64位 Apache httpd 最新包的安装
- VirtualBox Centos 网络设置