java实现httpclient发送post请求

红太狼 2022-06-04 02:22 542阅读 0赞

需求:现在要在java后端接口中直接请求客户提供的其他接口来获取所需要的数据,那么就需要用到httpclient来做,下面做一个实现以记录…

第一步:导入所需要的jar包并写一个工具类

Center

1.post请求工具类

因为我们需要的协议是https协议,所以我做了一个httpsPostUtil

  1. package com.qs.util;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.client.HttpClient;
  5. import org.apache.http.client.methods.HttpPost;
  6. import org.apache.http.util.EntityUtils;
  7. /**
  8. * 利用HttpClient进行post请求的工具类
  9. *
  10. * @author Devin <xxx>
  11. * @ClassName: HttpClientUtil
  12. * @Description: TODO
  13. * @date 2017年2月7日 下午1:43:38
  14. */
  15. public class HttpsPostUtil {
  16. public static String doPost(String url, String charset) {
  17. HttpClient httpClient = null;
  18. HttpPost httpPost = null;
  19. String result = null;
  20. try {
  21. httpClient = new SSLClient();
  22. httpPost = new HttpPost(url);
  23. HttpResponse response = httpClient.execute(httpPost);
  24. if (response != null) {
  25. HttpEntity resEntity = response.getEntity();
  26. if (resEntity != null) {
  27. result = EntityUtils.toString(resEntity, charset);
  28. }
  29. }
  30. } catch (Exception ex) {
  31. ex.printStackTrace();
  32. }
  33. return result;
  34. }
  35. }

2.用于进行Https请求的HttpClient

  1. package com.qs.util;
  2. import org.apache.http.conn.ClientConnectionManager;
  3. import org.apache.http.conn.scheme.Scheme;
  4. import org.apache.http.conn.scheme.SchemeRegistry;
  5. import org.apache.http.conn.ssl.SSLSocketFactory;
  6. import org.apache.http.impl.client.DefaultHttpClient;
  7. import javax.net.ssl.SSLContext;
  8. import javax.net.ssl.TrustManager;
  9. import javax.net.ssl.X509TrustManager;
  10. import java.security.cert.CertificateException;
  11. import java.security.cert.X509Certificate;
  12. /**
  13. * 用于进行Https请求的HttpClient
  14. *
  15. * @author Devin <xxx>
  16. * @ClassName: SSLClient
  17. * @Description: TODO
  18. * @date 2017年2月7日 下午1:42:07
  19. */
  20. public class SSLClient extends DefaultHttpClient {
  21. public SSLClient() throws Exception {
  22. super();
  23. SSLContext ctx = SSLContext.getInstance("TLS");
  24. X509TrustManager tm = new X509TrustManager() {
  25. public void checkClientTrusted(X509Certificate[] chain,
  26. String authType) throws CertificateException {
  27. }
  28. public void checkServerTrusted(X509Certificate[] chain,
  29. String authType) throws CertificateException {
  30. }
  31. public X509Certificate[] getAcceptedIssuers() {
  32. return null;
  33. }
  34. };
  35. ctx.init(null, new TrustManager[]{tm}, null);
  36. SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  37. ClientConnectionManager ccm = this.getConnectionManager();
  38. SchemeRegistry sr = ccm.getSchemeRegistry();
  39. sr.register(new Scheme("https", 443, ssf));
  40. }
  41. }

第二步:java中调用方式

  1. String url = "https://www.aaa.com/sss/?a=" + a + "&b=" + b + "&c=" + c + "&d=" + d;
  2. String httpsRtn = HttpsPostUtil.doPost(url, "UTF-8");
  3. Map<String, Object> json = mapper.readValue(httpsRtn, Map.class);
  4. String id = (String) json.get("id");

如果任何疑问或学习交流,请搜索公众号”老秦的快乐生活”获取我的联系方式

发表评论

表情:
评论列表 (有 0 条评论,542人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java实现发送post请求

    1 背景介绍 最近有一个任务,完成数据获取和解析,需要发送带请求参数的post请求,才能拿到数据。之前没有接触过java发送post请求,但有接触过python的requ