发送HTTP请求 -- HttpUtil
1.
package com.step.utils; import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
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.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSONObject; /**
* 发送HTTP请求
* @author mlu
*
*/
public class HttpUtils { /**
* 发送post请求--用于接口接收的参数为JSON字符串
* @param url 请求地址
* @param params json格式的字符串
* @return
*/
public static String httpPost(String url, String params){
String result = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
/*
* 发送json字符串,这两句需要设置
*/
httpPost.addHeader("Content-type","application/json; charset=utf-8");
httpPost.setHeader("Accept", "application/json"); httpPost.setEntity(new StringEntity(params, "UTF-8")); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) {
// Read the response body
result = EntityUtils.toString(response.getEntity(),"UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
} /**
* 发送post请求--用于接口接收的参数为键值对
* @param url 请求地址
* @param nameValuePairs 键值对
* @return
*/
public static String httpPost(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String strResult = ""; try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) {
/* 读返回数据 */
strResult = EntityUtils.toString(response.getEntity());
// System.out.println("conResult:"+conResult);
} else {
strResult += "发送失败:" + response.getStatusLine().getStatusCode();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return strResult;
} public static String httpGet(String url, List<NameValuePair> nameValuePairs){
HttpClient httpClient = new DefaultHttpClient();
String sb = "";
String result = "";
try {
for(NameValuePair nvp:nameValuePairs){
sb += nvp.getName()+"="+nvp.getValue()+"&";
}
sb = sb.substring(0,sb.length()-1);
sb = URLDecoder.decode(sb, "UTF-8");
HttpGet httpGet = new HttpGet(url+"?"+sb); HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
/* 读返回数据 */
result = EntityUtils.toString(response.getEntity());
} else {
result += "发送失败:" + response.getStatusLine().getStatusCode();
} } catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
} public static void main(String[] args) {
String url = "http://10.140.8.56/gd_fssc/rest/fsscService/getStaffInfo";
String url2 = "http://localhost:8080/eshore-app-backframe-web/interface/getJson"; // 发送 POST 请求
JSONObject json = new JSONObject();
json.put("number", "44053211@GD");
httpPost(url,json.toString()); List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("method", "login"));
httpGet(url2,nameValuePairs);
}
}
引用的jar包:
发送HTTP请求 -- HttpUtil的更多相关文章
- 高德地图web端笔记;发送http请求的工具类
1.查询所有电子围栏 package com.skjd.util; import java.io.BufferedReader; import java.io.InputStream; import ...
- java通过java.net.URL发送http请求调用接口
一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...
- Java发送Http请求并获取状态码
通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...
- AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
最近在使用AngularJs+Php开发中遇到php后台无法接收到来自AngularJs的数据,在网上也有许多解决方法,却都点到即止.多番摸索后记录下解决方法:tips:当前使用的AngularJ ...
- Ajax发送POST请求SpringMVC页面跳转失败
问题描述:因为使用的是SpringMVC框架,所以想使用ModelAndView进行页面跳转.思路是发送POST请求,然后controller层中直接返回相应ModelAndView,但是这种方法不可 ...
- 使用HttpClient来异步发送POST请求并解析GZIP回应
.NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...
- 在发送ajax请求时加时间戳或者随机数去除js缓存
在发送ajax请求的时候,为了保证每次的都与服务器交互,就要传递一个参数每次都不一样,这里就用了时间戳 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的 ...
- HttpUrlConnection发送url请求(后台springmvc)
1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...
- kattle 发送post请求
一.简介 kattle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定.它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述 ...
随机推荐
- Android 利用TimerTask实现ImageView图片播放效果
在项目开发中,往往 要用到图片播放的效果.今天就用TimerTask和ImageView是实现简单的图片播放效果. 当中,TimerTask和Timer结合一起使用.主要是利用TimerTask的迭代 ...
- 更改python字符编码以便使用UTF-8的编码url路径
url编码分两种, 一种是unicode, 另一种是gb2312, 今天遇到的一个网站是要将字符编码按照gb2312来编码,用来得到一个先填写blanks后再返回页面的数据,废话少说,需要做的就是先查 ...
- CentOS7开启网络配置
虚拟机在安装时可以开启网络 如果没有开启的话 可以通过以下操作 ip addr 查看是否开启网络 没有开启的话 cd /etc/sysconfig/network-scripts/ 然后 执行 ls ...
- Google浏览器vim命令
使用鼠标久了,手腕.肩膀依旧疼痛.偶尔逛知乎,看到有人推荐chrome浏览器的vimium插件(火狐浏览器是vimperator),安装了使用了几天,真不愧是浏览器神器,好用到想哭,而且非常容易上手. ...
- 关于数据未渲染完,要获取document高度问题——ajax全局事件
昨天在做开发时,遇到这样一个问题,当页面刚加载的时候,就要获取document的高度,可是此时页面上所有的ajax请求的数据都还没有渲染到页面上,所以导致得到的document的高度仅仅是页面结构的高 ...
- Android截图<包括Alertdiaog>
1.使用的系统内部的截屏方法…… 2. public class MainActivity extends AppCompatActivity { private static final Strin ...
- 请问具体到PHP的代码层面,改善高并发的措施有哪些
1.今天被问一个问题:请问具体到PHP的代码层面,改善高并发的措施有哪些? 面对高并发问题我首先想到的是集群.缓存(apt.redis.mem.内存...),但具体到PHP代码层面除了想到队列.减少网 ...
- shell编程-1.字符截取命令-列截取cut
- ZBrush软件特性之Marker标记调控板
在ZBrush®中使用Marker标记调控板来记忆物体属性,因此能在任何时间回到标记并使用它给其他物体或改变物体作为参考点. ZBrush软件下载:http://pan.baidu.com/s/1sl ...
- requests模块的高级用法
SSL Cert Verification #证书验证(大部分网站都是https) import requests respone=requests.get('https://www.12306.cn ...