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