Java利用httpclient通過get、post方式調用https接口的方法
通過httpclient的get post方式調用http很常見。一般都是
HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(http://127.0.0.1/login);
但是如果要調用https這個方式就不行了。就要修改DefaultHttpClient
<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>
先導入包
然后重寫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));
}
}
這時候就可以使用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.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調用代碼
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調用代碼
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參數(shù)說明
se.setContentEncoding(new BasicHeader(“Content-Type”, “application/json”));
使用的是json模式 所以傳的格式是json
application/xhtml+xml :XHTML格式
application/xml : XML數(shù)據(jù)格式
application/atom+xml :Atom XML聚合格式
application/json : JSON數(shù)據(jù)格式
application/pdf :pdf格式
application/msword : Word文檔格式
application/octet-stream : 二進制流數(shù)據(jù)(如常見的文件下載)
application/x-www-form-urlencoded : 中默認的encType,form表單數(shù)據(jù)被編碼為key/value格式發(fā)送到服務器(表單默認的提交數(shù)據(jù)的格式)
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);
如果要采用表單提交方式就需要修改成上面所描述的方式。
到此這篇關于Java利用httpclient通過get、post方式調用https接口的文章就介紹到這了,更多相關java調用https接口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot中ApplicationEvent和ApplicationListener用法小結
這篇文章介紹SpringBoot中ApplicationEvent用法,注意ApplicationEvent和MQ隊列雖然實現(xiàn)的功能相似,但是MQ還是有其不可替代性的,最本質的區(qū)別就是MQ可以用于不同系統(tǒng)之間的消息發(fā)布,而SpringEvent這種模式只能在一個系統(tǒng)中,需要的朋友可以參考下2023-03-03
SpringBoot使用Spring-Data-Jpa實現(xiàn)CRUD操作
這篇文章主要為大家詳細介紹了SpringBoot使用Spring-Data-Jpa實現(xiàn)CRUD操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
java中java.util.Date和java.sql.Date之間的轉換的示例
java.util.Date是java.sql.Date的父類,有時候在和SqlServer數(shù)據(jù)庫打交道時,也會遇到,本文主要介紹了java中java.util.Date和java.sql.Date之間的轉換的示例,具有一定的參考價值,感興趣的可以了解一下2024-05-05
spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25開發(fā)環(huán)境搭
這篇文章主要介紹了spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11+mysql5.5.25開發(fā)環(huán)境搭建圖文教程,需要的朋友可以參考下2016-06-06
Java如何使用JSR303校驗數(shù)據(jù)與自定義校驗注解
這篇文章主要介紹了Java如何使用JSR303校驗數(shù)據(jù)與自定義校驗注解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09

