java 发送https post请求
时间: 2023-05-01 08:01:39 浏览: 123
java支持https的Post请求
Java发送HTTPS POST请求可以使用java.net.URLConnection和javax.net.ssl.HttpsURLConnection类。具体代码示例如下:
```java
import java.io.*;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;
public class HttpsPost {
public static void main(String[] args) throws Exception {
// create the connection
URL url = new URL("https://example.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// send the request
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes("key1=value1&key2=value2");
out.flush();
out.close();
// read the response
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
}
```
注意:如果请求的网站是需要证书认证的,还需要额外处理证书。
阅读全文