JAVA调用Rest服务接口

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());

            }

        }

    }

 

posted @ 2016-01-11 22:38  zouhao510  阅读(16229)  评论(0编辑  收藏  举报