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

            }

        }

    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM