调用http接口(HttpClientUtil)

package com.threeclear.api.common.tool.method;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
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.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class HttpClientUtil {
	/**
	 * POST请求
	 * @param url
	 * @param map
	 * @return
	 */
    public static String HttpPost(String url, Map<String, String> map){
	     HttpClient httpClient = HttpClientBuilder.create().build();
	     HttpPost httpPost = new HttpPost(url);
		 List<NameValuePair> list = new ArrayList<NameValuePair>();
  		 if(map!=null && map.size()>0){
  		     Iterator iterator = map.entrySet().iterator();
	         while(iterator.hasNext()){
	             Entry<String,String> elem = (Entry<String, String>) iterator.next();
	             list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
	         }
  		 }

		try {
	        if(list.size() > 0){
	            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"utf-8");
	            httpPost.setEntity(entity);
	        }
           HttpResponse response = httpClient.execute(httpPost);
		    if(response != null){
               HttpEntity resEntity = response.getEntity();
               if(resEntity != null){
                  String result = EntityUtils.toString(resEntity,"utf-8");
                  return result;
               }  
           } 
      }catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}catch (ClientProtocolException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}  
	 return null;
   }
    /**
     * Get请求
     * @param url
     * @return
     */
    public static String HttpGet(String url) {
		HttpClient client = HttpClientBuilder.create().build();
		HttpGet request = new HttpGet(url);
		ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
			public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
				int status = response.getStatusLine().getStatusCode();
				if (status >= 200 && status < 300) {
					HttpEntity entity = response.getEntity();
					return entity != null ? EntityUtils.toString(entity) : null;
				} else {
					throw new ClientProtocolException("Unexpected response status: " + status);
				}
			}
		};
		try {
			String responseBody = client.execute(request, responseHandler);
			return responseBody;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 用于请求图片对应字节
	 * @param url
	 * @return
	 */
	public static byte [] HttpGetImage(String url)
	{
		HttpClient client = HttpClientBuilder.create().build();

		HttpGet request = new HttpGet(url);
		ResponseHandler<byte[]> responseHandler = new ResponseHandler<byte[]>() {
			public byte[] handleResponse(final HttpResponse response) throws IOException {
				int status = response.getStatusLine().getStatusCode();
				if (status >= 200 && status < 300) {
					HttpEntity entity = response.getEntity();
					return entity != null ? EntityUtils.toByteArray(entity) : null;
				} else {
					throw new ClientProtocolException("Unexpected response status: " + status);
				}
			}
		};
		try {
			byte[] responseBody = client.execute(request, responseHandler);
			return responseBody;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值