JAVA通过HTTPS发送POST请求的方法

因为调用一个外部接口,会用到POST请求,而且还是Https的,但是由于之前学习的时候没有用到,所以研究了很久才弄懂了怎么去用JAVA实现Https发送post请求

使用的是HttpsURLConnection来操作的


代码如下



import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;


import javax.net.ssl.HttpsURLConnection;


public class AutoFamilyAPITest {
public static void main(String[] args) throws Exception {
URL reqURL = new URL("xxxx"); //创建URL对象
HttpsURLConnection httpsConn = (HttpsURLConnection)reqURL.openConnection();

httpsConn.setDoOutput(true); 
httpsConn.setRequestMethod("POST");
OutputStreamWriter out = new OutputStreamWriter(httpsConn.getOutputStream()); 
out.write("xxxx"); 
out.flush(); 
out.close();

//取得该连接的输入流,以读取响应内容 
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());


//读取服务器的响应内容并显示
int respInt = insr.read();
while(respInt != -1){
System.out.print((char)respInt);
respInt = insr.read();
}
}
}










猜你喜欢

转载自blog.csdn.net/u013294097/article/details/80244206