发送HTTP请求 -- HttpUtil

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/es-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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值