惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
Last Week in AI
Last Week in AI
腾讯CDC
The Cloudflare Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
I
InfoQ
雷峰网
雷峰网
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
Y
Y Combinator Blog
H
Help Net Security
T
Tailwind CSS Blog
美团技术团队
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed

博客园 - 家中慢步

AKShare 高频请求东财数据接口的异常问题及解决方案 quartz 2.2.1 jdbc 连接池参数配置 redmine 安装roadmap 插件 centos 下自动备份redmine 数据 centos 5.6 安装redmine 步骤 解决mysql 写入中文读出乱码的问题 SVN的Redmine集成插件 Quartz.net Tutorial Lesson 2 Redmine 导入AD用户 RedMine 邮件通知配置 teamlab与redmine试用对比报告 Redmine集成LDAP认证 Redmine 初体验 jqgrid 属性说明 [原创]sql server inner join 效率测试 为sql server客户端连接添加别名 [转载]sql server T-SQL 区分字符串大小写 的两种方法 [转载]sql server 常用存储过程 Quartz.net Tutorial Lesson 1
httpclient发送request请求时设置header和timeout
家中慢步 · 2015-09-25 · via 博客园 - 家中慢步
package com.xxx.xxx.common;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpResponse;

import com.google.common.base.Charsets;

public class HttpRequest {
    /**
     * 向指定URL发送GET方法的请求
     * 
     * @param url
     *            发送请求的URL
     * @param param
     *            httprequest请求参数。
     * @param headers
     *            需要添加的httpheader参数
     * @param timeout
     *            请求超时时间
     * @return result 所代表远程资源的响应结果
     */
    public static String Get(String url, String param, Map<String, String> headers, int timeout) {
        String result = "";
        BufferedReader in = null;
        String reqUrl = url + "?" + param;
        try {
            // 构造httprequest设置
            RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout)
                    .setConnectionRequestTimeout(timeout).build();
            HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
            HttpGet htGet = new HttpGet(reqUrl);
            // 添加http headers
            if (headers != null && headers.size() > 0) {
                for (String key : headers.keySet()) {
                    htGet.addHeader(key, headers.get(key));
                }
            }
            // 读取数据
            HttpResponse r = client.execute(htGet);
            in = new BufferedReader(new InputStreamReader(r.getEntity().getContent(), Charsets.UTF_8));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in = null;
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
}

posted @ 2015-09-25 15:45  家中慢步  阅读(21426)  评论()    收藏  举报