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 ...
随机推荐
- String - 字符串分割操作
如果我想将一个字符串按照每8位一组分为若干个块,然后存储在一个byte[ ]数组中,我首先需要确定这个byte数组的长度,但由于我无法确定这个字符串的长度是否可以被8整除,所以无法直接判断,因此需要对 ...
- javascript中offsetWidth、clientWidth、width、scrollWidth、clientX、screenX、offsetX、pageX
原文:https://www.cnblogs.com/ifworld/p/7605954.html 元素宽高 offsetWidth //返回元素的宽度(包括元素宽度.内边距和边框,不包括外边距) o ...
- vue 实现简单的富文本编辑
安装:npm install vue-quill-editor --save 安装Vue-Quill-Editor需要依赖: npm install quill --save 在入口文件main.js ...
- Cygwin不能编译及解决办法
最近不知道什么原因,以前使用cygwin编译Android动态库,现在不能使用了,报下面的错误. Android NDK: Host 'awk' tool is outdated. Please de ...
- js正则验证表达式验证
/* 合法uri */ export function validateURL(textval) { const urlregex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\ ...
- codeforces Beautiful Numbers
来源:http://codeforces.com/problemset/problem/1265/B B. Beautiful Numbers You are given a permutat ...
- MySQLroot密码的恢复方法
MySQLroot密码的恢复方法 有可能你的系统没有 safe_MySQLd 程序(比如我现在用的 ubuntu操作系统, apt-get安装的MySQL) , 下面方法可以恢复 1.停止MySQLd ...
- C++实现对MySQL数据库的连接,以及增删改查
安装好MySQL,建好数据表的前提下. 如果只是想简单实现添加数据或者其他一个操作数据,可以参考另一篇博客. https://www.cnblogs.com/ming-4/p/11544514.htm ...
- Mysql 中使用 utfmb4 需要注意的问题
查资料时看到一个前人的经验总结,非常有用: http://seanlook.com/2016/10/23/mysql-utf8mb4/
- 老段带你学鸟哥Linux视频教程 包含基础班+提高班
老段带你学鸟哥Linux视频教程 包含基础班+提高班,附带pdf文档. 目录结构如下: 目录:/-老段带你学鸟哥Linux视频教程 [.9G] ┣━━老段带你学鸟哥-服务器篇 [1009.4M] ┃ ...