继续这个问题将aes-128-cbc php转换为java要将php代码迁移到java编程,我面临着如何在java中替换fsockopen的问题。
以下是php代码:
$sockHost = 'https://xxxx';
$port = '443'
$getPath = 'xxxx/xxx'
$getHost = 'xxx'
$data 'paramxxx'
$sock = fsockopen($sockHost, $port, $errNo, $errStr, 1000.0);
if ($sock == true) {
fwrite($sock, "POST " . $getPath . " HTTP/1.1\r\n");
fwrite($sock, "Host: " . $getHost() . "\r\n");
fwrite($sock, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($sock, 'Content-Length: ' . strlen($data) . "\r\n");
fwrite($sock, "Connection:close\r\n\r\n");
fwrite($sock, $data);
while (feof($sock) == false) {
$result .= fread($sock, 256);
}
fclose($sock);
下面是java代码:
private static void connect(String data,String host){
String https_url = "https://xxxx";
String host = "xxx";
String data= "xxx";
try {
HttpsURLConnection https_con = getHttpsClient(https_url);
https_con.setRequestProperty("Content-Length", String.valueOf(data.length()));
https_con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
https_con.setRequestMethod("POST");
https_con.setRequestProperty("Host",host);
https_con.setRequestProperty("Connection", "close");
https_con.setRequestProperty("Data",data);
https_con.setDoOutput(true);
https_con.connect();
int responseCode = https_con.getResponseCode();
try (BufferedReader in = new BufferedReader(new InputStreamReader(https_con.getInputStream()))) {
String line;
StringBuilder response = new StringBuilder();
while ((line = in.readLine()) != null) {
response.append(line).append("\n");
}
System.out.println(response.toString());
}
https_con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private static HttpsURLConnection getHttpsClient(String url) throws Exception {
// Security section START
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Security section END
HttpsURLConnection client = (HttpsURLConnection) new URL(url).openConnection();
//add request header
client.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36");
return client;
}
但是输出写入站点的所有内容,而不是发送请求到https的结果。
1条答案
按热度按时间oymdgrw71#
fsockopen(php)函数在tcp级别工作,而httpsurlconnection(java)函数在http级别工作(https://networkinterview.com/http-vs-tcp-know-the-difference )
java中的等效功能应为socket类,请查看:https://docs.oracle.com/javase/tutorial/networking/sockets/readingwriting.html