Java uses httpclient to call the method of https interface through get and post
- 2021-08-16 23:47:58
- OfStack
It is common to call http through get post of httpclient. 1 is generally
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(http://127.0.0.1/login);
But if you want to call https, this method will not work. DefaultHttpClient will be modified
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
Import the package first
Then override the class of DefaultHttpClient
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
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;
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 {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
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));
}
}
At this time, you can call it in https mode
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.HttpGet;
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 {
public static String doGet(String url,String charset) throws Exception{
HttpClient httpClient = null;
HttpGet Httpget = null;
String result = null;
httpClient = new SSLClient();
Httpget = new HttpGet(url);
Httpget.addHeader("Content-Type", "application/json");
HttpGet.setEntity(se);
HttpResponse response = httpClient.execute(Httpget);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
return result;
}
public static String doPost(String url,String json,String charset) throws Exception{
HttpClient httpClient = null;
HttpPost HttpPost = null;
String result = null;
httpClient = new SSLClient();
HttpPost = new HttpPost(url);
HttpPost.addHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(json);
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);
}
}
return result;
}
}
post calling code
public static void main(String[] args) throws Exception{
String url = "https://127.0.0.1/getuser";
String json = "{\"id\":1}";
String str = HttpClientUtil.doPost(url, json, "utf-8");
System.out.println(str);
}
get calling code
public static void main(String[] args) throws Exception{
String url = "https://127.0.0.1/getuser?id=1";
String str = HttpClientUtil.doPost(url, "utf-8");
System.out.println(str);
}
StringEntity Parameter Description
se. setContentEncoding (new BasicHeader ("Content-Type", "application/json"));
The json mode is used, so the format of transmission is json
application/xhtml+xml: XHTML Format
application/xml: XML data format
application/atom+xml: Atom XML Aggregation Format
application/json: JSON data format
application/pdf: pdf Format
application/msword: Word Document Format
application/octet-stream: Binary stream data (such as common file downloads)
application/x-www-form-urlencoded: The default encType, form form data is encoded to the key/value format and sent to the server (the default submission data format for the form)
HttpPost.addHeader("Content-Type", " application/x-www-form-urlencoded");
List<NameValuePair> params=new ArrayList<>();
params.add(new BasicNameValuePair("key1","value1"));
params.add(new BasicNameValuePair("key2","value2"));
params.add(new BasicNameValuePair("key3","value3"));
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"UTF-8");
HttpPost.setEntity(entity);
If you want to adopt the form submission method, you need to modify it to the method described above.