目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery post进行请求。

但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而jquery的post请求是不允许跨域的。

这时,就只能够用HttpClient包进行请求了,同时由于请求的URL是HTTPS的,为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程。

1.写一个SSLClient类,继承至HttpClient

  1. import java.security.cert.CertificateException;
  2. import java.security.cert.X509Certificate;
  3. import javax.net.ssl.SSLContext;
  4. import javax.net.ssl.TrustManager;
  5. import javax.net.ssl.X509TrustManager;
  6. import org.apache.http.conn.ClientConnectionManager;
  7. import org.apache.http.conn.scheme.Scheme;
  8. import org.apache.http.conn.scheme.SchemeRegistry;
  9. import org.apache.http.conn.ssl.SSLSocketFactory;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. //用于进行Https请求的HttpClient
  12. public class SSLClient extends DefaultHttpClient{
  13. public SSLClient() throws Exception{
  14. super();
  15. SSLContext ctx = SSLContext.getInstance("TLS");
  16. X509TrustManager tm = new X509TrustManager() {
  17. @Override
  18. public void checkClientTrusted(X509Certificate[] chain,
  19. String authType) throws CertificateException {
  20. }
  21. @Override
  22. public void checkServerTrusted(X509Certificate[] chain,
  23. String authType) throws CertificateException {
  24. }
  25. @Override
  26. public X509Certificate[] getAcceptedIssuers() {
  27. return null;
  28. }
  29. };
  30. ctx.init(null, new TrustManager[]{tm}, null);
  31. SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  32. ClientConnectionManager ccm = this.getConnectionManager();
  33. SchemeRegistry sr = ccm.getSchemeRegistry();
  34. sr.register(new Scheme("https", 443, ssf));
  35. }
  36. }

2.写一个利用HttpClient发送post请求的类

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.NameValuePair;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.message.BasicNameValuePair;
  13. import org.apache.http.util.EntityUtils;
  14. /*
  15. * 利用HttpClient进行post请求的工具类
  16. */
  17. public class HttpClientUtil {
  18. public String doPost(String url,Map<String,String> map,String charset){
  19. HttpClient httpClient = null;
  20. HttpPost httpPost = null;
  21. String result = null;
  22. try{
  23. httpClient = new SSLClient();
  24. httpPost = new HttpPost(url);
  25. //设置参数
  26. List<NameValuePair> list = new ArrayList<NameValuePair>();
  27. Iterator iterator = map.entrySet().iterator();
  28. while(iterator.hasNext()){
  29. Entry<String,String> elem = (Entry<String, String>) iterator.next();
  30. list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
  31. }
  32. if(list.size() > 0){
  33. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
  34. httpPost.setEntity(entity);
  35. }
  36. HttpResponse response = httpClient.execute(httpPost);
  37. if(response != null){
  38. HttpEntity resEntity = response.getEntity();
  39. if(resEntity != null){
  40. result = EntityUtils.toString(resEntity,charset);
  41. }
  42. }
  43. }catch(Exception ex){
  44. ex.printStackTrace();
  45. }
  46. return result;
  47. }
  48. }

3.调用post请求的测试代码

    1. import java.util.HashMap;
    2. import java.util.Map;
    3. //对接口进行测试
    4. public class TestMain {
    5. private String url = "https://192.168.1.101/";
    6. private String charset = "utf-8";
    7. private HttpClientUtil httpClientUtil = null;
    8. public TestMain(){
    9. httpClientUtil = new HttpClientUtil();
    10. }
    11. public void test(){
    12. String httpOrgCreateTest = url + "httpOrg/create";
    13. Map<String,String> createMap = new HashMap<String,String>();
    14. createMap.put("authuser","*****");
    15. createMap.put("authpass","*****");
    16. createMap.put("orgkey","****");
    17. createMap.put("orgname","****");
    18. String httpOrgCreateTestRtn = httpClientUtil.doPost(httpOrgCreateTest,createMap,charset);
    19. System.out.println("result:"+httpOrgCreateTestRtn);
    20. }
    21. public static void main(String[] args){
    22. TestMain main = new TestMain();
    23. main.test();
    24. }
    25. }

JAVA HttpClient进行POST请求(HTTPS)的更多相关文章

  1. java httpclient发送json 请求 ,go服务端接收

    /***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...

  2. Java httpClient 发送http请求

    RestTemplate ObjectMapper将string反序列化为WeatherResponse类 RestTemplate通过spring配置注入

  3. Android 实现 HttpClient 请求Https

    如题,默认下,HttpClient是不能请求Https的,需要自己获取 private static final int SET_CONNECTION_TIMEOUT = 5 * 1000; priv ...

  4. Java HttpClient伪造请求之简易封装满足HTTP以及HTTPS请求

    HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 jav ...

  5. [转]java 关于httpclient 请求https (如何绕过证书验证)

    原文:http://www.blogjava.net/hector/archive/2012/10/23/390073.html 第一种方法,适用于httpclient4.X 里边有get和post两 ...

  6. JAVA利用HttpClient进行POST请求(HTTPS)

    目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery post进行请求. 但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQuery的p ...

  7. java请求https地址如何绕过证书验证?

    原文http://www.blogjava.net/hector/archive/2012/10/23/390073.html 第一种方法,适用于httpclient4.X 里边有get和post两种 ...

  8. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  9. HttpClient 发送 HTTP、HTTPS 请求的简单封装

    import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.Http ...

随机推荐

  1. TextBox(只允许输入字母或者数字)——重写控件

    实现如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...

  2. Python网络爬虫-requests模块(II)

    有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们想要的目的,例如: #!/usr/bin/env ...

  3. python学习笔记--pycurl模块安装遇到的问题。

    1.用easy_install安装的时候 [root@idayuan ~]# easy_install pycurl Searching for pycurl Best match: pycurl A ...

  4. Install hadoop on windows(non-virtual machine, such cygwin)

    DownloadBefore starting make sure you have this two softwares Hadoop 2.7.1 Java – Jdk 1.7+ Extract d ...

  5. jps命令发生异常

    当在集群里输入jps命令时报如下错误: 我就开始检查jdk,感觉应该是centos自动的jdk没卸载干净跟后面安装的jdk冲突 先通过命令 rpm -qa|grep java 查看jdk信息 把这几个 ...

  6. python应用之爬虫实战1 爬虫基本原理

    知识内容: 1.爬虫是什么 2.爬虫的基本流程 3.request和response 4.python爬虫工具 参考:http://www.cnblogs.com/linhaifeng/article ...

  7. Linux编辑器|gedit|vi|vim编辑器

    gedit编辑器 gedit是一个Linux环境下的文本编辑器,类似windows下的写字板程序,在不需要特别复杂的编程环境下,作为基本的文本编辑器比较合适. sublime编辑器 Sublime T ...

  8. 折腾了几个小时,分享下zendstudio10的git使用

    今天打开zend10,发现新建项目的地方有 from git,from github,就试了试,发现可以导出,也可以commit,但是没办法push. 就百度百度,发现zendstudio10的git ...

  9. Some facts about topological sort

    Definition: a topological sort of a DAG G is a sort such that for all edge (i,j) in G, i precedes j. ...

  10. PHP依赖注入(DI)和控制反转(IoC)详解

    这篇文章主要介绍了PHP依赖注入(DI)和控制反转(IoC)的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 首先依赖注入和控制反转说的是同一个东西,是一种设计模式,这种设计模式用来减少程 ...