標籤:
有個互連網上的網址: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 協議