testng发送https的post请求
时间: 2023-09-26 16:06:20 浏览: 74
要发送HTTPS的POST请求,可以使用Java中的HttpURLConnection类和相关的类库。以下是一个示例代码,可以将其用作参考:
```java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpsPostRequest {
public static void main(String[] args) throws Exception {
String url = "https://example.com/api";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//设置请求方法为POST
con.setRequestMethod("POST");
//添加请求头
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
//设置请求参数
String urlParameters = "param1=value1¶m2=value2";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
//获取响应状态码
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
//读取响应内容
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());
}
}
```
在这个示例中,我们使用了HttpsURLConnection类来发送HTTPS请求,并设置了请求方法为POST。我们也设置了请求头和请求参数,然后获取响应状态码和响应内容。你可以将示例代码中的URL和参数替换为你自己的请求。
阅读全文