发送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上运行,数据抽取高效稳定.它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述 ...
随机推荐
- lightoj--1245--Harmonic Number (II)(数学推导)
Harmonic Number (II) Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...
- 杂项-电信:TL9000
ylbtech-杂项-电信:TL9000 TL9000是电信业质量体系要求(书1)与质量体系法则(书2)的指南, 它包括ISO9001的所有要求,以及硬件.软件, 服务方面行业的特别要求. 这些新增要 ...
- POJ 1414 暴搜
题意比较复杂 (但是很好理解) 大概意思是给你等边三角形(详见题目中的图). 最后一行有n个数,下一次要填的数是c. 里面预先已经填好了数字.(0为未填) 得分的标准是这个分数的连通块周围没有空的地方 ...
- Java Servlet 配置
图片太大,可以右键另存再查看,也可以鼠标点击拖置一下,用浏览器单独承载放大查看.
- jQuery分页插件pagination的用法
https://www.zhangxinxu.com/jq/pagination_zh/ 参数: 参数名 描述 参数值 maxentries 总条目数 必选参数,整数 items_per_page 每 ...
- CorelDRAW教程:怎样绘制制作箭头流程图?
箭头流程图主要由矢量图和连接符组成,通过图形之间的顺序阐述的一个过程,应用也是非常广泛,有些软件中会自带流程图,对于CDR这款矢量绘图软件来说,手动制作流程图是简单且高效的.首先CorelDRAW中就 ...
- hdu2614 Beat
题意: 有n个问题. 给出你解决完第i个问题之后解决j问题所花的时间,花的时间越多表示难度越大,每次只能解决难度大于或等于上个题难度的问题.问你最多能解决多少问题. 他妈的,第一次做想半天想不出来如何 ...
- CF #487 (Div. 2) D. A Shade of Moonlight 构造_数形结合
题意: 给 nnn个长度为 lll 且互不相交的开区间 (xi,xi+l)(x_{i}, x_{i}+l)(xi,xi+l) ,每个区间有一个移动速度 vvv,v∈1,−1v∈1,-1v∈1,−1 ...
- HDU 4630 No Pain No Game (线段树+离线)
题目大意:给你一个无序的1~n的排列a,每次询问[l,r]之间任取两个数得到的最大gcd是多少 先对所有询问离线,然后把问题挂在区间的左端点上(右端点也行) 在预处理完质数,再处理一个next数组,表 ...
- 【RHEL7/CentOS7防火墙之firewall-cmd命令详解】
目录 Firewalld zone firewall-cmd 开始配置防火墙策略 总结 Redhat Enterprise Linux7已默认使用firewalld防火墙,其管理工具是firewall ...