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

            }

        }

    }

 

转载于:https://www.cnblogs.com/zouhao510/p/5122813.html

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页