发送http请求和https请求的工具类

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));
}
}

原文地址:https://www.cnblogs.com/zhangzhiqin/p/9933978.html

时间: 2024-08-26 05:45:57

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

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

1.httpclient就是个能发送http连接的工具包,包括能发送post请求和get请求. http 连接一次就有返回流.http是个双向的嘛.只有连接了,就会有输出返回流. 所以在执行http连接的时候,返回值都是http连接的返回流. HttpResponse response = client.execute(httpPost); 2.http发送,body里是可以写入中文的.但要注意乱码问题: public static String getHttpRequestString(Str

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

一.http请求 1.http请求方式:get和post get一般用于获取/查询资源信息,在浏览器中直接输入url+请求参数点击enter之后连接成功服务器就能获取到的内容,post请求一般用于更新资源,通过form表单或者json.xml等其他形式提交给服务器端,然后等待服务器端给返回一个结果的方式(这个返回结果一般就是被修改之后的是否成功的状态,或者是修改后的最新数据table等). http请求,不论是get还是post请求,都会包含几个部分,分别是header,cookie,get会有

Charles抓包手机请求和Https请求

1.打开Charles,在Proxy里面点击Proxy Settings 2.在Proxy Settings的 Proxies添加端口 3.设置手机WLAN(注意Charles和手机连接的WIFi需在同一局域网) 4.长按连接的WIFI,出现修改网络,点击修改网络,点击显示高级选项 5.点击代理,选择手动 6.填写代理服务主机名(Charles安装电脑的ip)和代理服务器端口(Proxies添加端口),代理服务器端口需要和Proxies添加端口一致 7.以上为设置手机代理的步骤,设置成功后,如果

Http请求和Https请求的区别

HTTP(Hypertext Transfer Protocol Vertion )超文本传输协议,被用于在web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截获了传输报文,就可以读取内容,所以不建议传输一些敏感信息. 为了解决这一缺陷,需要使用另一种协议,安全套接字超文本传输协议(HTTPS),为了数据传输的安全,HTTPS在HTTP的基础上加入了SSL 协议,SSL依靠证书来检验服务器的身份,并且还为客户端与服务器之间的通信加密. 每次

第八节 request发送get请求和post请求

1 import requests 2 kw = {'wd':"中国"} 3 headers = { 4 "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" 5 } 6 resp = requests.get('http://www.baidu.co

HTTP的GET请求和POST请求

1.HTTP请求概述 在前面有一篇文章对HTTP协议又详细的描述,这里就不再过多的做说明,值是简单的作为这篇文章的引子. HTTP协议又被称为超文本传输协议,他的的设计目的是保证客户机与服务器之间的通信.HTTP 的工作方式是客户机与服务器之间的请求-应答协议.在客户机和服务器之间进行请求-响应时,有两个最基本的请求方式:GET 和 POST. 其中,GET请求表示从指定的资源请求数据,POST请求表示向指定的资源提交要被处理的数据. 2.HTTP请求格式 在HTTP请求中,首先是请求行,注意这

赵雅智_HttpClient的 get请求和post请求

DefaultHttpClient和它的兄弟AndroidHttpClient都是HttpClient具体的实现类,它们都拥有众多的API,而且实现比较稳定,bug数量也很少. 但同时也由于HttpClient的API数量过多,使得我们很难在不破坏兼容性的情况下对它进行升级和扩展,所以目前Android团队在提升和优化HttpClient方面的工作态度并不积极. 在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端.它提供了对HTTP协议的

Ajax相关——get请求和post请求的区别

一.完整的URL由以下几部分组成: scheme:通信协议,常用的有:http/ftp. host:主机,服务器(计算机)域名或IP地址 port:端口,整数,可选,省略时使用默认端口,http的默认端口是80. path:路径,由零或多个"/"符号隔开的字符,一般用来表示主机的一个目录或者文件地址 query:查询,可选,用于给动态网页(如用CGI.ISAPI.PHP/ASP/JSP/ASP.NET等技术制作的网页)传递参数,用"&"符号隔开,每个参数的名

前端ajax中运用post请求和get请求之于session验证

首先我们来看下ajax两种请求的区别: Ajax中POST和GET的区别Get和Post都是向服务器发送的一种请求,只是发送机制不同. 1. GET请求会将参数跟在URL后进行传递,而POST请求则是作为HTTP消息的实体内容发送给WEB服务器.当然在Ajax请求中,这种区别对用户是不可见的. 2. 首先是"GET方式提交的数据最多只能是1024字节",因为GET是通过URL提交数据,那么GET可提交的数据量就跟URL的长度有直接关系了.而实际上,URL不存在参数上限的问题,HTTP协