有个互联网上的网址: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程序来访问。html
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:java
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 协议是挺麻烦的。服务器
http://blog.csdn.net/today1858/article/details/5859876 spa
http://www.cnblogs.com/devinzhang/archive/2012/02/28/2371631.html.net