2011-04-20 61 views
1

如何將此PHP代碼轉換爲java?我如何進行HTTPS連接?curl Java中的HTTPS實現

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $to_post); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 40); 
$result = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 

我用這個HTTP着我使用類似

URL url = new URL("http://XZX"); 
      URLConnection con = url.openConnection(); 
      con.setDoInput(true); 
      con.setConnectTimeout(40000); 
      con.setDoOutput(true); 
      con.setUseCaches(false); 
      con.setDefaultUseCaches(false); 
      // tell the web server what we are sending 
      con.setRequestProperty("Content-Type", "text/xml;charset=\"utf-8\""); 
      con.setRequestProperty("Accept", "text/xml"); 
      con.setRequestProperty("Cache-Control", "no-cache"); 
      con.setRequestProperty("Pragma", "no-cache"); 
      con.setRequestProperty("SOAPAction", "\"run\""); 
      con.setRequestProperty("Content-length", String.valueOf(requestXml.length())); 
      OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); 
      writer.write(requestXml); 
      writer.flush(); 
      writer.close(); 
      InputStreamReader reader = new InputStreamReader(con.getInputStream()); 
+2

你試過谷歌:「https連接在java中」? – 2011-04-20 12:55:25

回答

2

東西你要考慮使用Apache HTTP Components。這具有您在Java中進行Web請求所需的全部功能。

如果您不想使用Apache庫,Java內置HttpsURConnection類用於使用HTTPS進行連接。

+0

更新的問題。 – user557348 2011-04-20 13:07:55

+0

我也會使用Apache的HTTP組件。他們現在甚至有一個流暢的界面,似乎不那麼冗長的HttpURLConnection – 2014-02-10 12:18:38

0

我不是PHP shaman,但它似乎在代碼示例中有一些PHP libcurl綁定。您可以使用Java libcurl綁定that you can download here對Java進行相同的修改。

test.java樣本如下:

test cw = new test(); 

// Register callback write function 
cg = new CurlGlue(); 
cg.setopt(CurlGlue.CURLOPT_WRITEFUNCTION, cw); 

// Login to the bank's secure Web site, posting account number and PIN code 
cg.setopt(CurlGlue.CURLOPT_URL, "https://www.santander.com.mx/SuperNetII/servlet/Login"); 
cg.setopt(CurlGlue.CURLOPT_SSLVERSION, iSSLVersion); 
cg.setopt(CurlGlue.CURLOPT_SSL_VERIFYPEER, bInsecureMode ? 0 : 1); 
cg.setopt(CurlGlue.CURLOPT_VERBOSE, bVerboseMode ? 1 : 0); 
cg.setopt(CurlGlue.CURLOPT_FOLLOWLOCATION, 1); 
cg.setopt(CurlGlue.CURLOPT_POST, 1); 
cg.setopt(CurlGlue.CURLOPT_COOKIEJAR, "cookie.txt"); 
cg.setopt(CurlGlue.CURLOPT_POSTFIELDS, "pag=login&pagErr=/index.html&usuario="+account+"&clave="+pinCode+"&irAmodulo=1"); 
cg.perform(); 

它看起來有點粗糙,但。 README提到只有簡單的API被實現,並且可能需要手動指定幾個int值。可能還需要一些努力來爲綁定正確配置環境,因爲您需要確保在環境的PATH中找到curl和ssl庫。它看起來像myset腳本是爲了照顧這一點。

+0

如何使它成爲一個jar文件? – NaviRamyle 2014-02-04 06:38:02

+0

首先,我會在所有Java文件的頂部添加一個package語句(類似於'package curl;',因爲現在它們位於根包中,並且這些類/方法很難從那裏導入。按照[這個簡單的教程](http://docs.oracle.com/javase/tutorial/deployment/jar/build.html)將文件打包到單個JAR中,也可以使用Eclipse Jar導出嚮導,if您正在使用此IDE。 – jlr 2014-02-04 06:47:50

+0

@Ramyle您是否能夠使其工作? – jlr 2014-02-06 19:38:54

1

您應該使用類HttpsURLConnection

但是,因爲你在curl

curl_setopt使用下列選項($ CH,CURLOPT_SSL_VERIFYPEER,0);

curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0);

您還需要設置HostNameVerifier,它不檢查主機名和不檢查服務器證書有效性的SSlSocketFactory。

它可以通過這個小樣本來完成:

// creating all trust manager. It will accept as valid any certificate. 
TrustManager[] trustAllCerts = new TrustManager[]{ 
    new X509TrustManager() { 
     public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
      return null; 
     } 
     public void checkClientTrusted(
      java.security.cert.X509Certificate[] certs, String authType) { 
     } 
     public void checkServerTrusted(
      java.security.cert.X509Certificate[] certs, String authType) { 
     } 
    } 
}; 
SSLContext sslContext = SSLContext.getInstance("SSL"); 
// set our friendly trust manager to ssl context. we pass it to connection later 
sslContext.init(null, trustAllCerts, null); 
HttpsURLConnection connection = (HttpsURLConnection) (url).openConnection(); 
// set all trust manager to connection 
connection.setSSLSocketFactory(sslContext.getSocketFactory()); 
// here we say not to check host name 
connection.setHostnameVerifier(new HostnameVerifier() { 
      @Override 
      public boolean verify(String s, SSLSession sslSession) { 
       return true; 
      } 
     }); 

後,您可以設置標題,超時,例如正如你已經做URLConnection