httpClient 表单提交通用模板

Java Web中涉及大量的请求调用,常用的方式一般就是RPC和Http两种方式,本篇主要利用Apache的httpClient对表单提交,抽象出一个httpProxy统一适配模板

一. pom依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.1.2</version>
</dependency>

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.2.2</version>
</dependency>

二. Java实现

public <T>T httpProxy(String ip,int port,,String url,Map<String,T> requestBody,Map<String,T>headerMap)  
        org.apache.http.HttpHost proxy = new org.apache.http.HttpHost(ip, port);
        url = proxy + url;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = null;
        if (method.equals(Const.GET_METHOD)) { //GET提交
            HttpGet httpGet = new HttpGet(url);

            // 处理请求头
            for (Map.Entry<String, T> map : headerMap.entrySet()) {
                httpGet.setHeader(map.getKey(), map.getValue());
            }
            response = httpClient.execute(httpGet);
        } else if (method.equals(Const.POST_METHOD)) {//POST提交
            HttpPost httpPost = new HttpPost(url);
            /// 处理请求体
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            for (Map.Entry<String, T> map : requestBody.entrySet()) {
                params.add(new BasicNameValuePair(map.getKey(), map.getValue()));
            }
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            // 处理请求头
            for (Map.Entry<String, T> map : headerMap.entrySet()) {
                httpPost.setHeader(map.getKey(), map.getValue());
            }
            response = httpClient.execute(httpPost);
        } else {
            response = null;
        }

        HttpEntity entity = response.getEntity();
        org.apache.http.Header[] headers = response.getAllHeaders();

        List<org.apache.http.Header> headerList = new ArrayList<org.apache.http.Header>();
        for (org.apache.http.Header header : headers) {
            headerList.add(header);
        }
         int code = response.getStatusLine().getStatusCode();
        InputStream in = entity.getContent();
        String responseBody = IOUtils.toString(in, HTTP.UTF_8);
        httpClient.getConnectionManager().shutdown();

        return new T(code, headerList, responseBody...);
    }

说明:
1. 用泛型代表返回类型可以自行封装
2. form表单提交默认只支持get跟post,其他方式在SpringMVC中需要另加配置http的methodfilter
3. 模板中省去了日志的输出
4. 各个类的源jar不能导错
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,156评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,993评论 6 342
  • 学习是一种什么样的体验? 学习对我来说,是醍醐灌顶的自我顿悟,来自更高场域的人对你说的一句话,让你瞬间开启了更大的...
    蔡蔡1988阅读 448评论 0 3
  • socket套接字: 网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字。 socke...
    frankisbaby阅读 358评论 0 0
  • 很喜欢喜欢这部电影个性轻快的背景音乐,暖色系的色调,别致、时段错开的爱情讲述方式。这不是一段完美结局的爱情...
    马修的学生阅读 414评论 0 0