JAVA HttpClient进行POST请求(HTTPS)
目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery post进行请求。
但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而jquery的post请求是不允许跨域的。
这时,就只能够用HttpClient包进行请求了,同时由于请求的URL是HTTPS的,为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程。
1.写一个SSLClient类,继承至HttpClient
- 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;
- //用于进行Https请求的HttpClient
- 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));
- }
- }
2.写一个利用HttpClient发送post请求的类
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- /*
- * 利用HttpClient进行post请求的工具类
- */
- public class HttpClientUtil {
- public String doPost(String url,Map<String,String> map,String charset){
- HttpClient httpClient = null;
- HttpPost httpPost = null;
- String result = null;
- try{
- httpClient = new SSLClient();
- httpPost = new HttpPost(url);
- //设置参数
- List<NameValuePair> list = new ArrayList<NameValuePair>();
- Iterator iterator = map.entrySet().iterator();
- while(iterator.hasNext()){
- Entry<String,String> elem = (Entry<String, String>) iterator.next();
- list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
- }
- if(list.size() > 0){
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
- httpPost.setEntity(entity);
- }
- HttpResponse response = httpClient.execute(httpPost);
- if(response != null){
- HttpEntity resEntity = response.getEntity();
- if(resEntity != null){
- result = EntityUtils.toString(resEntity,charset);
- }
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }
- return result;
- }
- }
3.调用post请求的测试代码
- import java.util.HashMap;
- import java.util.Map;
- //对接口进行测试
- public class TestMain {
- private String url = "https://192.168.1.101/";
- private String charset = "utf-8";
- private HttpClientUtil httpClientUtil = null;
- public TestMain(){
- httpClientUtil = new HttpClientUtil();
- }
- public void test(){
- String httpOrgCreateTest = url + "httpOrg/create";
- Map<String,String> createMap = new HashMap<String,String>();
- createMap.put("authuser","*****");
- createMap.put("authpass","*****");
- createMap.put("orgkey","****");
- createMap.put("orgname","****");
- String httpOrgCreateTestRtn = httpClientUtil.doPost(httpOrgCreateTest,createMap,charset);
- System.out.println("result:"+httpOrgCreateTestRtn);
- }
- public static void main(String[] args){
- TestMain main = new TestMain();
- main.test();
- }
- }
JAVA HttpClient进行POST请求(HTTPS)的更多相关文章
- java httpclient发送json 请求 ,go服务端接收
/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...
- Java httpClient 发送http请求
RestTemplate ObjectMapper将string反序列化为WeatherResponse类 RestTemplate通过spring配置注入
- Android 实现 HttpClient 请求Https
如题,默认下,HttpClient是不能请求Https的,需要自己获取 private static final int SET_CONNECTION_TIMEOUT = 5 * 1000; priv ...
- Java HttpClient伪造请求之简易封装满足HTTP以及HTTPS请求
HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 jav ...
- [转]java 关于httpclient 请求https (如何绕过证书验证)
原文:http://www.blogjava.net/hector/archive/2012/10/23/390073.html 第一种方法,适用于httpclient4.X 里边有get和post两 ...
- JAVA利用HttpClient进行POST请求(HTTPS)
目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery post进行请求. 但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQuery的p ...
- java请求https地址如何绕过证书验证?
原文http://www.blogjava.net/hector/archive/2012/10/23/390073.html 第一种方法,适用于httpclient4.X 里边有get和post两种 ...
- 【JAVA】通过HttpClient发送HTTP请求的方法
HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.Http ...
随机推荐
- 关于android im
从各种pack中看到 环信 easemob.com 300万用户以下免费 org.jivesoftware.smackopenfireappkefu等开源im
- 关于Redis的那些事
1. MySql+Memcached架构的问题 Memcached采用客户端-服务器的架构,客户端和服务器端的通讯使用自定义的协议标准,只要满足协议格式要求,客户端Library可以用任何语言实现. ...
- BASIC-20_蓝桥杯_数的读法
示例代码: #include <stdio.h>#include <string.h>#define N 10 char num[N] = {0} ; void yuyin(i ...
- 【Spring实战-2】Spring4.0.4整合Hibernate4.3.6
作者:ssslinppp 源程序下载:http://download.csdn.net/detail/ssslinppp/8751185 1. 摘要 本文主要讲解如何在Spring4.0. ...
- spring SOA architecture
在谈这个之前,还得再说下SOA和平台.SOA做两件事情,一个是解耦并识别可重用的服务,一个是对服务进行灵活组装和编排满足业务需求,SOA核心是业务和技术的解耦,服务和能力的复用.而在IT领域的平台平台 ...
- Access restriction: The type Resource is not accessible due to restriction on required library
方法一: 全局属性Project>preferences>java>Compiler>Errors/Warnings>把右侧的[Deprecated and restri ...
- 1048 Find Coins (25 分)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...
- 使用V$SQL_PLAN视图获取曾经执行过的SQL语句执行计划
通常我们查看SQL语句的执行计划都是通过EXPLAIN PLAN或者AUTOTRACE来完成.但是这些查看方法有一个限制,它们都是人为触发而产生的,无法获得数据库系统中曾经执行过的SQL语句执行计划. ...
- sqoop产生背景及概述
sqoop产生背景 多数是用Hadoop技术处理大数据业务的企业有大量的数据存储在传统的关系型数据库(RDBMS)中:由于缺乏工具的支持.对Hadoop和传统数据库系统中的数据进行相互传输是一件十分困 ...
- Servlet、Servlet容器等内容讲解
转载自http://blog.csdn.net/iAm333 对于Servlet.Servlet容器以及一个Servlet容器-Tomcat这些概念讲解的挺清晰的,转载下 之前在开源中国看到一篇文章& ...