码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA调用Rest服务接口

时间:2016-01-11 23:32:45      阅读:2310      评论:0      收藏:0      [点我收藏+]

标签:

package com.zouhao.dfx.web.controller;

/**
 * Created by zouhao on 16/1/11.
 *
 * @ClassName: ${ClassName}
 * @Description: TODO
 * @author: zouhao
 * @date: 16/1/11
 */


import java.io.*;

import java.net.*;

public class RestUtil {

    public String load(String url,String query) throws Exception
    {

    URL restURL = new URL(url);

        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();

        conn.setRequestMethod("POST");

        conn.setDoOutput(true);

        conn.setAllowUserInteraction(false);

        PrintStream ps = new PrintStream(conn.getOutputStream());
        ps.print(query);

        ps.close();

        BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String line,resultStr="";

        while(null != (line=bReader.readLine()))

        {

        resultStr +=line;

        }

        bReader.close();

        return resultStr;

    }

    public static void main(String []args) {

        try {

            RestUtil restUtil = new RestUtil();

            String resultString = restUtil.load("http://58.33.45.112:port/TrafficflowCommon/Handler.ashx","param1=d¶m2=e");

            } catch (Exception e) {

            // TODO: handle exception

            System.out.print(e.getMessage());

            }

        }

    }

 

JAVA调用Rest服务接口

标签:

原文地址:http://www.cnblogs.com/zouhao510/p/5122813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!