curl在Java中实现HTTPS实现

时间:2011-04-20 12:52:32

标签: java php https

如何将此PHP代码转换为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());

3 个答案:

答案 0 :(得分:2)

您需要考虑使用Apache HTTP Components。这具有使用Java进行Web请求所需的所有功能。

如果您不想使用Apache库,Java有一个内置的HttpsURConnection类,可以使用HTTPS进行连接。

答案 1 :(得分: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做的那样

答案 2 :(得分:0)

我不是PHP萨满,但似乎你的代码示例中有一些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脚本就是为了解决这个问题。