如何使用java代码发送https请求
时间: 2023-06-05 11:04:33 浏览: 492
您可以使用Java的HttpsURLConnection类来发送HTTPS请求。以下是一个简单的示例代码:
```
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HttpsURLConnection;
public class HttpsURLConnectionExample {
public static void main(String[] args) {
try {
String url = "https://www.example.com";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
// 可选:设置请求头
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
// 可选:设置代理
System.setProperty("https.proxyHost", "proxy.example.com");
System.setProperty("https.proxyPort", "8080");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
阅读全文