How do I convert this PHP code to java? How do I make an HTTPS connection?
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);
I use this for HTTP cant i use something similar
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-len开发者_如何学运维gth", String.valueOf(requestXml.length()));
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(requestXml);
writer.flush();
writer.close();
InputStreamReader reader = new InputStreamReader(con.getInputStream());
You'll want to look into using Apache HTTP Components. This has all the functionality you need for making web requests in Java.
If you don't want to use the Apache library, Java has a built in HttpsURConnection class for connecting using HTTPS.
You should use class HttpsURLConnection
But, because you use following options in curl
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
you need also set HostNameVerifier which don't check host name and SSlSocketFactory which don't check server certificate validity.
It can be done using this little sample:
// 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;
}
});
after you can set headers, timeouts, e.g. as you already do for URLConnection
I'm no PHP shaman, but it seems you got some PHP libcurl bindings in your code sample. You can do the same in Java with minor changes using the Java libcurl bindings that you can download here.
A sample from the test.java
looks like this:
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();
It looks a bit rough though. The README mentions that only the simple API is implemented and specifying a few int values manually might be required. It might take some effort to properly configure the environment for the bindings too, as you'll need to make sure the curl and ssl libraries are found in the PATH of your environment. It looks like the myset
scripts are meant to take care of this.
精彩评论