練習 讀取函式裡的值

weixin_34119545發表於2015-01-16
String getInput(){   
        String str="";
        try  
        {  
        URL url = new URL("http://www.baidu.com/");  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        conn.setDoInput(true);  
        conn.setConnectTimeout(10000);  
        conn.setRequestMethod("GET");  
        conn.setRequestProperty("accept", "*/*");  
        String location = conn.getRequestProperty("location");  
        int resCode = conn.getResponseCode();  
        conn.connect();  
        InputStream stream = conn.getInputStream();  
        byte[] data=new byte[102400];  
        int length=stream.read(data);  
         str=new String(data,0,length);   
        conn.disconnect();  
        System.out.println(str);  
        stream.close();  
        
        }  
        catch(Exception ee)  
        {  
        System.out.print("ee:"+ee.getMessage());   
        } 
        return str; //返回str
        } 

 

  

呼叫方法           String a= getInput();

相關文章