可用:java调用post,可用:https,http,传递Heade(可无),传递json(可无),根据业务逻辑使用;
工具类1:FormatEmpty,判断是不是空
import java.util.List;
import java.util.Map;
public class FormatEmpty {
public static boolean isEmpty(Object str) {
if (str == null || isEmpty(str.toString())) {
return true;
}
return false;
}
/**
* 判断是否为空(null||length==0)
*
* @param str
* @return false不为空,true为空
*/
public static boolean isEmpty(String str) {
if (str == null || str.trim().length() == 0) {
return true;
}
return false;
}
/**
* 判断是否为空(null||isEmpty)
*
* @param map
* @return false不为空,true为空
*/
public static boolean isEmpty(Map<?, ?> map) {
if (map == null || map.isEmpty()) {
return true;
}
return false;
}
/**
* 判断是否为空(null||isEmpty)
*
* @param list
* @return false不为空,true为空
*/
public static boolean isEmpty(List<?> list) {
if (list == null || list.isEmpty()) {
return true;
}
return false;
}
/**
* 判断是否为空(null||length == 0)
*
* @param array
* @return false不为空,true为空
*/
public static boolean isEmpty(Object[] array) {
if (array == null || array.length == 0) {
return true;
}
return false;
}
}
工具类1:SSLClient,去除证书
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
@SuppressWarnings("deprecation")
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception {
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
post请求调用:
maven
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.11</version>
</dependency>
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URL;
/**
* @ClassName post_util
* @Date 2020/1/3 16:08
**/
public class post_util {
/**
* url:可为https或http,判断是否有证书
* token:可以为空,为空时Heade为空
* json:为json字符串
*/
public static String postRequest(String url, String token,String json) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = null;
HttpPost HttpPost = new HttpPost(url);
URL realUrl = new URL(url);
if(!FormatEmpty.isEmpty(token)){
//可继续新增addHeader
HttpPost.addHeader("access-token", token);
}
if(!FormatEmpty.isEmpty(json)){
StringEntity s = new StringEntity(json, "UTF-8");
s.setContentType("application/json;charset=UTF-8");
s.setContentEncoding("UTF-8");
HttpPost.setEntity(s);
}
// 设置请求器的配置
HttpResponse response = null;
try {
if (realUrl.getProtocol().toLowerCase().equals("https")) {
// 忽略所有的SSL请求的证书
httpClient = new SSLClient();
}
response = httpClient.execute(HttpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
} finally {
HttpPost.abort();
}
return result;
}
}
测试:
public static void main(String[] args) throws Exception {
//也可https
String pst = post_util.postRequest("http://ip/platform/user" ,"token" ,"{\"s\":\"ss\"}");
}
ps : 传递参数参考: https://blog.csdn.net/qq_41712834/article/details/101201588
有什么不清楚的可以评论哦。
才疏学浅 ,请多指教