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 ...
随机推荐
- TextBox(只允许输入字母或者数字)——重写控件
实现如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...
- Python网络爬虫-requests模块(II)
有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们想要的目的,例如: #!/usr/bin/env ...
- python学习笔记--pycurl模块安装遇到的问题。
1.用easy_install安装的时候 [root@idayuan ~]# easy_install pycurl Searching for pycurl Best match: pycurl A ...
- 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 ...
- jps命令发生异常
当在集群里输入jps命令时报如下错误: 我就开始检查jdk,感觉应该是centos自动的jdk没卸载干净跟后面安装的jdk冲突 先通过命令 rpm -qa|grep java 查看jdk信息 把这几个 ...
- python应用之爬虫实战1 爬虫基本原理
知识内容: 1.爬虫是什么 2.爬虫的基本流程 3.request和response 4.python爬虫工具 参考:http://www.cnblogs.com/linhaifeng/article ...
- Linux编辑器|gedit|vi|vim编辑器
gedit编辑器 gedit是一个Linux环境下的文本编辑器,类似windows下的写字板程序,在不需要特别复杂的编程环境下,作为基本的文本编辑器比较合适. sublime编辑器 Sublime T ...
- 折腾了几个小时,分享下zendstudio10的git使用
今天打开zend10,发现新建项目的地方有 from git,from github,就试了试,发现可以导出,也可以commit,但是没办法push. 就百度百度,发现zendstudio10的git ...
- 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. ...
- PHP依赖注入(DI)和控制反转(IoC)详解
这篇文章主要介绍了PHP依赖注入(DI)和控制反转(IoC)的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 首先依赖注入和控制反转说的是同一个东西,是一种设计模式,这种设计模式用来减少程 ...