httpclient post 请求
package com.thinkgem.jeesite.common.utils; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONObject; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.*; public class HttpUtil {
HttpClient httpclient=new DefaultHttpClient(); /**
* 发送 post请求访问本地应用并根据传递参数不同返回不同结果
*/
public String post(String url,String reqEncoding,String respEncoding,JSONObject jsonObj) {
String resStr = "";
// 创建httppost
HttpPost httppost = new HttpPost(
url);
// 创建参数队列
//List<NameValuePair> formparams = jsonObj; UrlEncodedFormEntity uefEntity;
try { StringEntity sEntity = new StringEntity(jsonObj.toString(),"utf-8");//解决中文乱码问题
sEntity.setContentEncoding("UTF-8");
sEntity.setContentType("application/json"); //httppost.setEntity(uefEntity);
httppost.setEntity(sEntity);
HttpResponse response;
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
resStr = EntityUtils.toString(entity,respEncoding);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
// httpclient.getConnectionManager().shutdown();
}
return resStr;
} /**
* 发送 get请求
*/
public String get(String url) {
String resStr = "";
try {
// 创建httpget.
HttpGet httpget = new HttpGet(url);
// 执行get请求.
HttpResponse response = httpclient.execute(httpget);
// 获取响应实体
HttpEntity entity = response.getEntity();
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) { resStr=EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
//httpclient.getConnectionManager().shutdown();
}
return resStr;
} public static void main(String[] args) throws ClientProtocolException, IOException {
/*HttpUtil ht = new HttpUtil();// 构造参数
String requestTime = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
requestTime = requestTime
.replaceFirst("" + requestTime.charAt(10), "T");//替换空为T requestTime += ".000";//增加毫秒数 ApiAuthUtil apiAuthUtil = new ApiAuthUtil("InterFlight", "Query",
"FzSearch", "4874257a-c262-4586-bd4b-09b95a500857",
"47dc925ec0a2ec96", requestTime, 96870);
String apiURL = apiAuthUtil
.getApiURL("http://tcopenapi.17usoft.com/flight/Inter/Query/FzSearch");
System.out.println(apiURL);
String result = ht.post(apiAuthUtil.getApiURL("http://tcopenapi.17usoft.com/flight/Inter/Query/FzSearch"), "utf-8");
System.out.println("result:" + result);*/ }
}
httpclient post 请求的更多相关文章
- 使用HttpClient发送请求、接收响应
使用HttpClient发送请求.接收响应很简单,只要如下几步即可. 1.创建HttpClient对象. CloseableHttpClient httpclient = HttpClients.c ...
- Java HttpClient伪造请求之简易封装满足HTTP以及HTTPS请求
HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 jav ...
- 使用HttpClient发送请求接收响应
1.一般需要如下几步:(1) 创建HttpClient对象.(2)创建请求方法的实例,并指定请求URL.如果需要发送GET请求,创建HttpGet对象:如果需要发送POST请求,创建HttpPost对 ...
- 使用httpclient post请求中文乱码解决办法
使用httpclient post请求中文乱码解决办法 在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码 ...
- java HttpClient POST请求
一个简单的HttpClient POST 请求实例 package com.httpclientget; import java.awt.List; import java.util.ArrayLis ...
- java HttpClient GET请求
HttpClient GET请求小实例,先简单记录下. package com.httpclientget; import java.io.IOException; import org.apache ...
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
1.转自:https://blog.csdn.net/alinshen/article/details/78221567?utm_source=blogxgwz4 /* * 演示通过HttpClie ...
- 给HttpClient添加请求头(HttpClientFactory)
前言 在微服务的大环境下,会出现这个服务调用这个接口,那个接口的情况.假设出了问题,需要排查的时候,我们要怎么关联不同服务之间的调用情况呢?换句话就是说,这个请求的结果不对,看看是那里出了问题. 最简 ...
- HttpClient get请求获取数据流
HttpClient get请求获取数据流,将数据保存为文件 public String getStreamFile(String url) throws Exception { HttpClient ...
- httpclient: 设置请求的超时时间,连接超时时间等
httpclient: 设置请求的超时时间,连接超时时间等 public static void main(String[] args) throws Exception{ //创建httpclien ...
随机推荐
- 离线安装paramiko以及pysftp linux,windows
离线安装 paramiko python37 pip download -d ./dependency -r requirements.txt pip install --no-index --ign ...
- javascipt——jQuery
1.首先需要导入jQuery1.12.4.js文件,把文件和当前目录放同一目录下.在<body>中写上<script scr = 'jQuery1.12.4.js'><s ...
- Comparable
为什么会有Comparable与Comparator接口? 引入策略模式 目录 引入 Comparable接口的来龙去脉 引入Comparator接口 什么是策略模式? 使用了策略模式有什么好处? ...
- IIS-简介
参考:https://www.jb51.net/article/85909.htm IIS是什么 iis是用来做什么的? IIS全程为Internet Information Service(In ...
- 【原】python-jenkins信息
1.官方文档: https://python-jenkins.readthedocs.io/en/latest/examples.html#example-1-get-version-of-jenki ...
- yii2.0框架安装
通过 Composer 安装 如果还没有安装 Composer,你可以按 getcomposer.org 中的方法安装. 在 Linux 和 Mac OS X 中可以运行如下命令: curl -sS ...
- github 创建gitlab每次提交都要输入账号
在使用git提交代码到github的时候,经常要求输入用户名和密码,类似这种: 除了在 github 上添加 SSH key 网上有这么一种解决方法:使用git提交到github,每次都要输入用户名和 ...
- wsgiref模块
学习django框架之前,可以先学习一下wsgiref模块,熟悉前后端交互. 一.先介绍下wsgiref模块 WSGI(Web Server Gateway Interface)是一种规范,它定义了使 ...
- PHP高级面试题
在网上看到一些高级php 的面试题目.. 最近接连面试了几家公司,有些重要问题记录一下,督促自己学习提高,同时希望给朋友们一些帮助.内容很多,一点点完善,一步步学习..有些是面试被问,有些是招聘要求, ...
- Linux - TLCL
三. 文件系统中跳转 pwd - Print name of current working directory cd - Change directory ls - List directory c ...