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的更多相关文章

  1. 高德地图web端笔记;发送http请求的工具类

    1.查询所有电子围栏 package com.skjd.util; import java.io.BufferedReader; import java.io.InputStream; import ...

  2. java通过java.net.URL发送http请求调用接口

    一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...

  3. Java发送Http请求并获取状态码

    通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...

  4. AngularJs的$http发送POST请求,php无法接收Post的数据解决方案

      最近在使用AngularJs+Php开发中遇到php后台无法接收到来自AngularJs的数据,在网上也有许多解决方法,却都点到即止.多番摸索后记录下解决方法:tips:当前使用的AngularJ ...

  5. Ajax发送POST请求SpringMVC页面跳转失败

    问题描述:因为使用的是SpringMVC框架,所以想使用ModelAndView进行页面跳转.思路是发送POST请求,然后controller层中直接返回相应ModelAndView,但是这种方法不可 ...

  6. 使用HttpClient来异步发送POST请求并解析GZIP回应

    .NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...

  7. 在发送ajax请求时加时间戳或者随机数去除js缓存

    在发送ajax请求的时候,为了保证每次的都与服务器交互,就要传递一个参数每次都不一样,这里就用了时间戳 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的 ...

  8. HttpUrlConnection发送url请求(后台springmvc)

    1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...

  9. kattle 发送post请求

    一.简介 kattle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定.它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述 ...

随机推荐

  1. caffe:使用C++来提取任意一张图片的特征

    0x00 关于使用C++接口来提取特征,caffe官方提供了一个extract_features.cpp的例程,但是这个文件的输入是blob数据,即使输入层使用的是ImageData,也需要在depl ...

  2. 8. String to Integer[M]字符串转整数

    题目 Inplement atoi which converts a string to an integer. The function first discards as many whitesp ...

  3. FMDB条件更新

    更新操作返回一个BOOL值,YES表示操作成功,NO表示执行过程遇到错误,可以通过-lastErrorMessage和-lastErrorCode查看错误信息.使用executeUpdate:方法执行 ...

  4. 如何在maven项目中使用spring

    今天开始在maven项目下加入spring. 边学习边截图. 在这个过程中我新建了一个hellospring的项目.于是乎从这个项目出发开始研究如何在maven项目中使用spring.鉴于网上的学习资 ...

  5. ROS-多机通信

    前言:一定要在同一路由的局域网下进行,就是两台电脑的ip要像这样:192.168.191.4和192.168.191.8,只有最后一位不同,这样就能ping通了,否则ping不同. 一.查看ip和主机 ...

  6. C#操作sql时注意点

    ①创建必要的索引 ②使用预编译查询 ③使用参数化sql会执行预编译,第一次执行的时候DBMS会为这个SQL语句进行查询优化并执行预编译 ④调整where子句中的连接顺序 ⑤DBMS一般次用自上而下的顺 ...

  7. (转载)Android项目实战(二十七):数据交互(信息编辑)填写总结

    Android项目实战(二十七):数据交互(信息编辑)填写总结   前言: 项目中必定用到的数据填写需求.比如修改用户名的文字编辑对话框,修改生日的日期选择对话框等等.现总结一下,方便以后使用. 注: ...

  8. onTouchEvent事件

    @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTIO ...

  9. 0-NULL-nullptr

    NULL In C A null-pointer constant is an integral constant expression that evaluates to zero (like 0 ...

  10. swift使用查阅资料备份2

    Swift3.0朝圣之路-Then协议库-绝妙的初始化方式 https://www.jianshu.com/p/6cc1e21df6ac DisposeBag http://southpeak.git ...