package com.haiyisoft.cAssistant.utils;

import java.io.IOException;
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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/*
* 利用HttpClient进行post请求的工具类
*/
public class HttpClientUtil {
public static 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 "888888";/**链接失败*/
}
return result;
}

public String doGet(String url, Map<String, String> param) {

// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();

String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
java.net.URI uri = builder.build();

// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);

// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状濁是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(),
"iso8859-1");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}

==========================================

package com.haiyisoft.cAssistant.utils;

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
@SuppressWarnings("deprecation")
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));
}
}

发送http请求和https请求的工具类的更多相关文章

  1. httpclient就是个能发送http连接的工具包,包括能发送post请求和get请求

    1.httpclient就是个能发送http连接的工具包,包括能发送post请求和get请求. http 连接一次就有返回流.http是个双向的嘛.只有连接了,就会有输出返回流. 所以在执行http连 ...

  2. python——请求服务器(http请求和https请求)

    一.http请求 1.http请求方式:get和post get一般用于获取/查询资源信息,在浏览器中直接输入url+请求参数点击enter之后连接成功服务器就能获取到的内容,post请求一般用于更新 ...

  3. JAVA HTTP请求和HTTPS请求

    HTTP与HTTPS区别:http://blog.csdn.net/lyhjava/article/details/51860215 URL发送 HTTP.HTTPS:http://blog.csdn ...

  4. haproxy 中的http请求和https请求

    use Mojolicious::Lite; use JSON qw/encode_json decode_json/; use Encode; no strict; use JSON; # /foo ...

  5. java中模拟http(https)请求的工具类

    在java中,特别是java web中,我们经常需要碰到的一个场景是我们需要从服务端去发送http请求,获取到数据,而不是直接从浏览器输入请求网址获得相应.比如我们想访问微信接口,获取其返回信息. 在 ...

  6. 使用SoapUI工具做get请求和post请求接口测试

    祝大家节日快乐啦. 之前写过的一篇帖子已经介绍了SoapUI工具的基本使用,所以在此不再重复讲解关于建工程.建测试套件.添加用例等操作,可查看该篇文章详解:http://www.cnblogs.com ...

  7. SoapUI工具做get请求和post请求接口测试

    转载自:https://www.cnblogs.com/hong-fithing/p/7617366.html 此篇主要介绍SoapUI工具做常用的两种请求接口测试,分别是get请求和post请求. ...

  8. GET 请求和 POST 请求的区别和使用

    作为前端开发, HTTP 中的 POST 请求和 GET 请求是经常会用到的东西,有的人可能知道,但对其原理和如何使用并不特别清楚,那么今天来浅谈一下两者的区别与如何使用. GET请求和POST请求的 ...

  9. Ajax中get请求和post请求

    我们在使用Ajax向服务器发送数据时,可以采用Get方式请求服务器,也可以使用Post方式请求服务器,那么什么时候该采用Get方式,什么时候该采用Post方式呢? Get请求和Post请求的区别: 1 ...

随机推荐

  1. JVM类加载机制和内存模型

    一.JVM类加载机制 首先需要了解一下类加载器(ClassLoader): ClassLoader是Java的一个核心组件,它主要作用是从系统外部获得Class二进制数据流,然后将数据流装载到系统,交 ...

  2. SQLplus命令中删除键和翻页键不能用的问题

    问题现象: 在进入连接数据库后,如何写错命令,删除键不好使,总是出现^H^H [oracle@master2 ~]$ sqlplus / as sysdba SQL*Plus: Release 12. ...

  3. 微软宣布加入机密计算联盟,与谷歌和BAT 等巨头联手保护数据安全

    联盟创始成员还包括阿里巴巴.Arm.百度.谷歌.IBM.英特尔.红帽.瑞士电信和腾讯等科技公司,它提供了一个让行业聚集起来的机会,以促进使用机密计算来更好地保护数据. 建立机密计算联盟的需求源于这样一 ...

  4. C++ 批量打开写入文件

    用到了C++17的filesystem 库 说明:这个函数主要是用来处理日志中不同Thread的日志,主要目的是将不同Thread的日志写到不同的文件中 int GetThreadTime(const ...

  5. 用cubase制作消音伴奏

  6. 【ARC072 E】Alice in linear land

    被智商题劝退,告辞 题意 有一个人在一条数轴的距离原点为 \(D\) 的位置,他可以执行 \(n\) 次操作,每次操作为给定一个整数 \(d_i\),这个人向原点的方向走 \(d_i\) 个单位,但如 ...

  7. KMP算法详解&&P3375 【模板】KMP字符串匹配题解

    KMP算法详解: KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt(雾)提出的. 对于字符串匹配问题(such as 问你在abababb中有多少个 ...

  8. zzzphp V1.6.0 按照功能分析漏洞

    0 基础支撑功能 0.1 路由功能 0.2 模版解析 * zzzphp V1.6.0 的代码执行漏洞,模版解析功能的问题 程序解析模版时,将模版中的部分内容匹配出来直接传递给了eval,且没有经过过滤 ...

  9. Gym - 101908J Joining Capitals (斯坦纳树)

    题意:二维平面上有n(n<=100)个点,其中k个是星星(k<=10),现要构造一棵树,每个星星对应树上的一个叶子结点,求最小花费(总花费为树上所有边的长度(两点间欧几里得距离)) 斯坦纳 ...

  10. 移动端H5不常见兼容收集

    1.微信分身input不支持上传,无法监听到上传事件 解决方案:判断在微信浏览器端使用微信JSSDK上传