时间:2021-05-20
通过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参数说明
se.setContentEncoding(new BasicHeader(“Content-Type”, “application/json”));
使用的是json模式 所以传的格式是json
application/xhtml+xml :XHTML格式
application/xml : XML数据格式
application/atom+xml :Atom XML聚合格式
application/json : JSON数据格式
application/pdf :pdf格式
application/msword : Word文档格式
application/octet-stream : 二进制流数据(如常见的文件下载)
application/x-ameValuePair("key3","value3"));UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"UTF-8");HttpPost.setEntity(entity);
如果要采用表单提交方式就需要修改成上面所描述的方式。
到此这篇关于Java利用httpclient通过get、post方式调用https接口的文章就介绍到这了,更多相关java调用https接口内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在java开发中我们会经常的使用到外部资源,而外资源一般是通过接口的方式提供出来,那在java中我们怎么使用HttpClient实现post请求接口实现实现We
HTTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求,下面我将结合HttpClient来实现一
一个发送短信验证码的功能,使用的是信易通的短信平台接口,然后在Java中使用HttpClient模拟POST请求或者GET请求(看短信平台要求,一般的情况下都是
为什么用restful接口?怎么用呢?restful接口常用的两种方式是get和post.下面简单介绍一下这两种方式的使用.由于调用restfu
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请