zoukankan      html  css  js  c++  java
  • HttpClient的简单实现

    /**
         *
         * 依赖的夹包:coommons-httpclient-3.1.jar  commons-codec-1.7.jar
         * @param url
         * @param 参数是: url, json:json格式的字符串
         * @return
         */
        public static String doPost(String url,String json){
            String response="";
          

            //创建HttpClient对象
            HttpClient httpClient = new HttpClient();
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(50000); // 连接5秒超时
            httpClient.getHttpConnectionManager().getParams().setSoTimeout(70000);// 读取30秒超时
     
            //通过PostMethod来创建链接,生成一个post请求
            PostMethod method=new PostMethod(url);
            try {

                 RequestEntity r=new StringRequestEntity(json);
                //RequestBody或是RequestEntity作用是提供传参,
                //method.setRequestBody(json);//只对整个body
                method.setRequestEntity(r);//可以是分段的内容,RequestEntity是一个接口,有很多实现可以传递不同类型的的参数
                //允许客户端或服务器中任何一方关闭底层的连接双方都会要求在处理请求后关闭它们的TCP连接
                method.setRequestHeader("Connection", "close");
                method.setRequestHeader("Content-type", "application/json; charset=UTF-8");
                //通过httpClient实例里的方法来实例化method
                httpClient.executeMethod(method);
                //读 response,返回的结果
                response = method.getResponseBodyAsString();
            } catch (HttpException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                //释放链接
                method.releaseConnection();
            }    
            return response;
        }

    ======================

    注解:HttpClient使用的步骤一般是如下:

    1. 创建 HttpClient 的实例

    2. 创建某种连接方法的实例method 实例,例如 GetMethod、PostMethod, 在 method 实例 的构造函数中传入要连接的地址

    3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
    4. 读 response,获取返回数据
    5. 释放连接。无论执行方法是否成功,都必须释放连接
    6. 对得到后的内容进行处理

    另外:PostMethod提供了方法来传递参数:setRequestBody和setRequestEntity(r)

    RequestEntity的好处是有许多实现类来处理不同的资源

    例如:你的参数是个流可以用InputStreamRequestEntity来构造RequestEntity

           如果参数是字符串可以用StringRequestEntity来构造RequestEntity

          如果是文件可以用FileRequestEntity来构造RequestEntity,还有其他的可以百度一下

    ====================实现方法如下====================

    我是用的是Java,框架是spring+springMVC+mybitis

    /**
     * 测试http请求
     * http://localhost:80/autopart/testhttp/testhttp1.do
     */
    @RequestMapping("/testhttp")
    @Controller
    public class TestHttpController {
        @RequestMapping(value="/testhttp1")
        @ResponseBody
        public String testHello(HttpServletRequest request ,HttpServletResponse response){
            //String url="http://localhost:8080/blind/testhttp/testhttp1.do";
            String url="http://localhost:80/autopart/testhttp/testhttp2.do";
            String json= "{"id":1001,"name":"蓝星"}";
            String returns=HttpUtils.doPost(url, json);
            System.err.println(returns);
            return returns;
        }
        @ResponseBody
        @RequestMapping(value = "/testhttp2", method = {
                RequestMethod.GET, RequestMethod.POST }, produces = "application/json;charset=utf-8")
        public String testHello(@RequestBody Map map){
            String name=(String)map.get("name");
            System.out.println("id是:"+10+",name是:"+name);
            return "id是:"+10+",name是:"+name;
        }
    }

    注解:

    我测试的方法是:写了一个测试类,通过第一个方法调用本类里的第二个方法通过http,调通就可以了

    另外:上面第一个方法中注释的url,是我调用的别的项目里的方法,也调通了

    大概做法是:在我的电脑上(Windows),D盘和F盘分别装了两个tomcat,启动两个eclipce,一个引用了D盘的tomcate,一个引用了F盘的tomcate

    在两个eclipce中分别运行blind项目和autopart项目,从autopart项目中调用blind的方法,参数传到了blind项目的方法中,并得到了返回的信息打印在了autopart控制台上

    这就简单实现了:同一个系统上不同服务器上的不同项目之间的信息传递,使用httpClient

  • 相关阅读:
    C库中与时间相关的函数
    读书笔记之: 程序员的自我修养——链接,装载与库(第1,2部分)
    STL中常用的一些算法
    C++ STL中的迭代器技术
    程序员面试宝典2(数据结构与算法)
    C/C++程序员面试宝典2
    读书笔记之: 数据库系统概论(第4版)
    C库中重要字符串函数的简要分析及实例
    程序员求职成功路(3)
    C库中对函数的可变参数的支持
  • 原文地址:https://www.cnblogs.com/xueershewang/p/6850037.html
Copyright © 2011-2022 走看看