码迷,mamicode.com
首页 > 编程语言 > 详细

从Java代码中访问 HTTPS 协议

时间:2015-03-17 00:56:32      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

有个互联网上的网址:https://wtsz.jyzq.cn/ywcl.jsp?type=l&yybdm=1100&market=Z&userName=11009341&pwd=870221&ip=3.3.3.3&serverName=jyzq.cn,是HTTPS协议的,如何通过JAVA程序能够调用该地址得到正确的返回数据。

当前这个地址是可以通过浏览器访问的,需要在后台通过JAVA程序来访问。

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class Test {

	public static void main(String[] args) throws IOException {
		URL reqURL = new URL(
				"https://wtsz.jyzq.cn/ywcl.jsp?type=l&yybdm=1100&market=Z&userName=11009341&pwd=870221&ip=3.3.3.3&serverName=jyzq.cn"); // 创建URL对象
		HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL
				.openConnection();

		/*
		 * 下面这段代码实现向Web页面发送数据,实现与网页的交互访问 httpsConn.setDoOutput(true);
		 * OutputStreamWriter out = new
		 * OutputStreamWriter(huc.getOutputStream(), "8859_1"); out.write( "……"
		 * ); out.flush(); out.close();
		 */

		// 取得该连接的输入流,以读取响应内容
		InputStreamReader insr = new InputStreamReader(
				httpsConn.getInputStream());

		// 读取服务器的响应内容并显示
		int respInt = insr.read();
		while (respInt != -1) {
			System.out.print((char) respInt);
			respInt = insr.read();
		}

	}

}

Output:

0##P8ZU08YAE77TFB9T4HRC93ZGBOZNSHDY


Groovy version:

import javax.net.ssl.HttpsURLConnection
    
        URL reqURL = new URL(
                "https://wtsz.jyzq.cn/ywcl.jsp?type=l&yybdm=1100&market=Z&userName=11009341&pwd=870221&ip=3.3.3.3&serverName=jyzq.cn"); // 创建URL对象
        HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL
                .openConnection();

        /*
         * 下面这段代码实现向Web页面发送数据,实现与网页的交互访问 httpsConn.setDoOutput(true);
         * OutputStreamWriter out = new
         * OutputStreamWriter(huc.getOutputStream(), "8859_1"); out.write( "……"
         * ); out.flush(); out.close();
         */

        // 取得该连接的输入流,以读取响应内容
        InputStreamReader insr = new InputStreamReader(
                httpsConn.getInputStream());

        // 读取服务器的响应内容并显示
        int respInt = insr.read();
        while (respInt != -1) {
            System.out.print((char) respInt);
            respInt = insr.read();
        }
//Output: 
//0##NQRCHIG6G7WJWMLKI5F1ETEGINNWT44X

注意:我用的 JAVA 8 64位版本。 据说以前在 JAVA 中, 访问 HTTPS 协议是挺麻烦的。

java发https请求,证书配置

http://blog.csdn.net/today1858/article/details/5859876

Java安全通信:HTTPS与SSL

http://www.cnblogs.com/devinzhang/archive/2012/02/28/2371631.html



从Java代码中访问 HTTPS 协议

标签:

原文地址:http://my.oschina.net/u/553266/blog/387722

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!