`
qinya06
  • 浏览: 584452 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

restful webserice

 
阅读更多
package com.onetown.action.admin;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 11-7-12
 * Time: 下午4:59
 * To change this template use File | Settings | File Templates.
 */
public class TestFUL {
    public static  void main(String[] args){
        URL url= null;
        try {
            url = new URL("http://localhost:8080/struts2-rest-showcase/orders");
        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        HttpURLConnection con = null;
        try {
            con = (HttpURLConnection)url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
            con.setRequestMethod("GET");
        } catch (ProtocolException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
            con.connect();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        InputStream in= null;
        try {
            in = con.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        byte[] b= new byte[1024];
        int result= 0;
        try {
            result = in.read(b);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        while( result!=-1){
            System.out.write(b,0,result);
            try {
                result= in.read(b);
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics