目前,要为另一个项目提供接口,接口是用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. 关于android im

    从各种pack中看到 环信 easemob.com  300万用户以下免费 org.jivesoftware.smackopenfireappkefu等开源im

  2. 关于Redis的那些事

    1.  MySql+Memcached架构的问题 Memcached采用客户端-服务器的架构,客户端和服务器端的通讯使用自定义的协议标准,只要满足协议格式要求,客户端Library可以用任何语言实现. ...

  3. BASIC-20_蓝桥杯_数的读法

    示例代码: #include <stdio.h>#include <string.h>#define N 10 char num[N] = {0} ; void yuyin(i ...

  4. 【Spring实战-2】Spring4.0.4整合Hibernate4.3.6

    作者:ssslinppp      源程序下载:http://download.csdn.net/detail/ssslinppp/8751185  1. 摘要 本文主要讲解如何在Spring4.0. ...

  5. spring SOA architecture

    在谈这个之前,还得再说下SOA和平台.SOA做两件事情,一个是解耦并识别可重用的服务,一个是对服务进行灵活组装和编排满足业务需求,SOA核心是业务和技术的解耦,服务和能力的复用.而在IT领域的平台平台 ...

  6. Access restriction: The type Resource is not accessible due to restriction on required library

    方法一: 全局属性Project>preferences>java>Compiler>Errors/Warnings>把右侧的[Deprecated and restri ...

  7. 1048 Find Coins (25 分)

    1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...

  8. 使用V$SQL_PLAN视图获取曾经执行过的SQL语句执行计划

    通常我们查看SQL语句的执行计划都是通过EXPLAIN PLAN或者AUTOTRACE来完成.但是这些查看方法有一个限制,它们都是人为触发而产生的,无法获得数据库系统中曾经执行过的SQL语句执行计划. ...

  9. sqoop产生背景及概述

    sqoop产生背景 多数是用Hadoop技术处理大数据业务的企业有大量的数据存储在传统的关系型数据库(RDBMS)中:由于缺乏工具的支持.对Hadoop和传统数据库系统中的数据进行相互传输是一件十分困 ...

  10. Servlet、Servlet容器等内容讲解

    转载自http://blog.csdn.net/iAm333 对于Servlet.Servlet容器以及一个Servlet容器-Tomcat这些概念讲解的挺清晰的,转载下 之前在开源中国看到一篇文章& ...