apach httpclien发起http请求


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.http.util.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.test.weather.Weather.ResultsBean;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class HttpUtils {
public static void main(String[] args) {
		//封装请求参数  
        List<NameValuePair> httpParams = Lists.newArrayList();
        httpParams.add(new BasicNameValuePair(“xxx”, "xxx"));
        httpParams.add(new BasicNameValuePair(“xxx”, "xxx"));  
        sendGetWithParam(url, httpParams);
	}

/**
	 * 发送http请求
	 */
	public static void sendGetWithParam(String url,List<NameValuePair> params) {
		CloseableHttpClient httpClient = HttpClients.createDefault();  
        CloseableHttpResponse response = null;  
        InputStream is = null;  
        
        String str = "";  
        try {  
            //转换为键值对  
            str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));  
            System.out.println("params:"+str);  
            //创建Get请求  
            HttpGet httpGet = new HttpGet(url+"?"+str);  
            System.out.println("url:"+(url+"?"+str));
            //执行Get请求,  
            response = httpClient.execute(httpGet);  
            //得到响应体  
            HttpEntity entity = response.getEntity();  
            if(entity != null){  
                is = entity.getContent();
                //转换为字节输入流  
                BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));  
                String body = null;  
                while((body=br.readLine()) != null){  
                    System.out.println("http body:"+body);
                }  
            }
        } catch (ParseException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally{  
            //关闭输入流,释放资源  
            if(is != null){  
                try {  
                    is.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            //消耗实体内容  
            if(response != null){  
                try {  
                    response.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            //关闭相应 丢弃http连接  
            if(httpClient != null){  
                try {  
                    httpClient.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值