/***************************网络编程***************************************/
public static String inputStream2String(InputStream is) throws IOException //输入流转换为string
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1)
{
baos.write(i);
}
return baos.toString();
}
public static void main(String[] args) {
int c;
URL url;
try {
//url = new URL("http://www.whsvc.com.cn");
url=new URL("http://www.baidu.com");
URLConnection hpCon=url.openConnection();//用地址的url创建连接
int len=hpCon.getContentLength();
System.out.println("Content-leng:"+len);
if(len>0){
System.out.println("--------------content-------------");
InputStream input=hpCon.getInputStream();
String str=inputStream2String(input); //将inputStream转换成string
System.out.println(str); //输出页面的文本内容,为html代码;
int i=len;
}else{
System.out.println("连接不可用!!!");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}