String sUrl = "http://www.sina.com";
HttpURLConnection conn =null;
try{
conn =(HttpURLConnection)new URL(sUrl).openConnection();
int result= conn.getResponseCode();
String re=conn.getResponseMessage();
if(result==HttpURLConnection.HTTP_OK){
System.out.println(re);
System.out.println("连接成功!");
}else{
System.out.println("连接不成功!");
}
}catch(Exception e ){
e.printStackTrace();
}finally{
if(conn!=null){
conn.disconnect();
conn = null;
}
}
URL 访问与返回
最新推荐文章于 2022-03-18 10:58:11 发布