发送HTTP请求 -- HttpUtil
来源:互联网 发布:zookeeper 默认端口 编辑:程序博客网 时间:2024/06/08 17:35
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包:
0 0
- 发送HTTP请求 -- HttpUtil
- Http请求工具类 httputil
- Http请求工具类HttpUtil
- HttpUtil工具类发送post请求
- httputil用http获取请求的工具类
- HttpUtil--Json发送
- HttpUtil 跨项目请求
- HttpUtil-get请求-post请求
- C#发送HTTP请求
- java 发送http请求
- socket 发送http请求
- python发送HTTP请求
- 异步发送HTTP请求
- python发送HTTP请求
- Qt发送HTTP请求
- socket发送http请求
- 发送http请求
- JAVA发送HTTP请求
- oracle 查询杀死死锁方案
- SQL Server执行计划的理解
- C++ Primer Plus(第六版)读书笔记(二)
- 【Java核心技术学习】Java数据类型
- adapter中操作listview之外的View 注意事项
- 发送HTTP请求 -- HttpUtil
- win32 edit控件尾部追加内容
- Java Web开发环境配置
- HDU1021 Fibonacci Again
- CGLIB 和 JDK生成动态代理类的区别
- Dito 手指尖的创意
- 欢迎使用CSDN-markdown编辑器
- Neural Turing Machines-NTM系列(一)简述
- 2015年终总结