您的位置:首页 > 编程语言 > Java开发

java模拟get/post请求

2016-01-26 21:51 471 查看
一、背景介绍
    1、有时本系统和第三方系统交互时会涉及get和post类型的请求。
二、实现逻辑
    1、GET方式的请求和浏览器中差不多,都要把参数附加到url后边。主要用的是getMethod封装和httpClient调用的exacuteMethod方法执行getMethod。
    2、POST方式和GET方式不同,它主要是使用NameValuePair数组结构将参数对象封装,然后用PostMethod封装url路径,其他的都相似。
三、具体实现

    1、GET请求
         String responseMsg = "";  
        // 1.构造HttpClient的实例  
        HttpClient httpClient = new HttpClient();  
        // 用于测试的http接口的url  
        String url="http://183.129.138.123:8080/mobile/mobile/open/fund/records?param1="+param1+"¶m2="+param2;  

        // 2.创建GetMethod的实例  
        GetMethod getMethod = new GetMethod(url);  

        // 使用系统系统的默认的恢复策略  
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  
                new DefaultHttpMethodRetryHandler());  

        try {  
            //3.执行getMethod,调用http接口  
            httpClient.executeMethod(getMethod);  

            //4.读取内容  
            byte[] responseBody = getMethod.getResponseBody();  

            //5.处理返回的内容  
            responseMsg = new String(responseBody);  
            log.info(responseMsg);  

        } catch (HttpException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            //6.释放连接  
            getMethod.releaseConnection();  
        }  
        return responseMsg;  
    2、POST请求
        String responseMsg = "";  

        //1.构造HttpClient的实例  
        HttpClient httpClient=new HttpClient();  

        httpClient.getParams().setContentCharset("UTF-8");  

        String url = URL + path;  

        //2.构造PostMethod的实例  
        PostMethod postMethod=new PostMethod(url);  

        //3.把参数值放入到PostMethod对象中  
        //方式1:  
        /*NameValuePair[] data = { new NameValuePair("param1", param1), 
                new NameValuePair("param2", param2) };*/ 
        postMethod.setRequestBody(data);  

        //方式2:      
        //postMethod.addParameter("param1", param1);  
        //postMethod.addParameter("param2", param2);  

        try {  
            // 4.执行postMethod,调用http接口  
            httpClient.executeMethod(postMethod);//200  

            //5.读取内容  
            responseMsg = postMethod.getResponseBodyAsString(); 
            log.info(responseMsg);  

            //6.处理返回的内容  

        } catch (Exception e) {  
           throw e; 
        } finally {  
            //7.释放连接  
            postMethod.releaseConnection();  
        }  
        return responseMsg;  
    }  
3、POST的测试方法
    /** 
     * 测试的main方法 
     * @param args 
     */  
    public static void main(String[] args) {  
/*        NameValuePair[] data = new NameValuePair[6];
        String path = "";  //设置预约平台访问路径

        data[0] = new NameValuePair("scheduleDate", "20160118");
        data[1] = new NameValuePair("timeSegStr", "");
        data[2] = new NameValuePair("typeStr", "");
        data[3] = new NameValuePair("deptId", "0");
        data[4] = new NameValuePair("doctorId", "0");
        data[5] = new NameValuePair("partnerToken", "69A4EC1F153141AECF10845E8E841142");
        path = "/schedule/searchSchedule"; 
        String responseJson = postHttp(data, path);

        System.out.println("post方式调用http接口\n" + responseJson);  */
        NameValuePair[] data = new NameValuePair[3];
        String path = "";  //设置预约平台访问路径

        //data[0] = new NameValuePair("token", "4FE03VV7ACP9128CEP9C09XIAMW710SV09F3CVSF8CE7Z0B892BSYA0XFL1Z04A");
        data[0] = new NameValuePair("hospitalId", "10266");
        data[1] = new NameValuePair("startDate", "2015-11-01 12:00:00");
        data[2] = new NameValuePair("endDate", "2016-01-22 12:00:00");
        path = "mobile-web/open/trade/fund/record"; 
        String responseJson;
        try {
            responseJson = postHttp(data, path);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
4、需要的JAR的maven配置

<dependency>

<groupId>commons-httpclient</groupId>

<artifactId>commons-httpclient</artifactId>

<version>3.1</version>

</dependency>

<dependency>

<groupId>commons-codec</groupId>

<artifactId>commons-codec</artifactId>

<version>1.2</version>

</dependency>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息