Java怎么调用HTTPS的接口

主机教程 建站分享 1年前 (2022-12-10) 137次浏览

文章摘要:Java怎么调用HTTPS的接口

在Java中使用HttpClient调用HTTPS接口,具体方法如下: import org.apache.h […]

在Java中使用HttpClient调用HTTPS接口,具体方法如下:

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.StatusLine;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.message.BasicHeader;

import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

@SuppressWarnings("resource")

public static String doPost(String url,String jsonstr,String charset){

HttpClient httpClient = null;

HttpPost httpPost = null;

String result = null;

try{

httpClient = new SSLClient();

httpPost = new HttpPost(url);

httpPost.addHeader("Content-Type", "application/json");

StringEntity se = new StringEntity(jsonstr);

se.setContentType("text/json");

se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));

httpPost.setEntity(se);

HttpResponse response = httpClient.execute(httpPost);

if(response != null){

HttpEntity resEntity = response.getEntity();

if(resEntity != null){

result = EntityUtils.toString(resEntity,charset);

}

}

}catch(Exception ex){

ex.printStackTrace();

}

return result;

}

}


声明:
若非注明,本站文章源于互联网收集整理和网友分享发布,如有侵权,请联系站长处理。
文章名称:Java怎么调用HTTPS的接口
文章链接:https://www.7966.org/post/14150.html
转载请注明出处

喜欢 (0)