java程序HttpUrlConnection类中的方法,调用返回状态码
try{URLpageUrl=newURL("http://www.baidu.com/");HttpURLConnectionhttpUrlConn=(HttpURLC...
try {
URL pageUrl = new URL("http://www.baidu.com/");
HttpURLConnection httpUrlConn = (HttpURLConnection)pageUrl.openConnection();
int statusCode = httpUrlConn.getResponseCode();
System.out.println(statusCode);
} catch (Exception e) {
e.printStackTrace();
}
问题是如果主域名无法访问就直接报错 而不是404 这是为啥
用这样写 也是报错 显示是没有权限
try {
URL pageUrl = new URL("http://www.baidu.com");
HttpURLConnection urlCon = new HttpURLConnection(pageUrl) {
@Override
public void connect() throws IOException {
// TODO Auto-generated method stub
}
@Override
public boolean usingProxy() {
// TODO Auto-generated method stub
return false;
}
@Override
public void disconnect() {
// TODO Auto-generated method stub
}
};
urlCon.getResponseCode();
System.out.println("返回的状态码是:");
} catch (Exception e) {
e.printStackTrace();
}
我知道我很小白 求高手帮帮忙 指点下 该去查哪方面的资料 或者是给讲讲 谢谢 没分了 好桑心啊 展开
URL pageUrl = new URL("http://www.baidu.com/");
HttpURLConnection httpUrlConn = (HttpURLConnection)pageUrl.openConnection();
int statusCode = httpUrlConn.getResponseCode();
System.out.println(statusCode);
} catch (Exception e) {
e.printStackTrace();
}
问题是如果主域名无法访问就直接报错 而不是404 这是为啥
用这样写 也是报错 显示是没有权限
try {
URL pageUrl = new URL("http://www.baidu.com");
HttpURLConnection urlCon = new HttpURLConnection(pageUrl) {
@Override
public void connect() throws IOException {
// TODO Auto-generated method stub
}
@Override
public boolean usingProxy() {
// TODO Auto-generated method stub
return false;
}
@Override
public void disconnect() {
// TODO Auto-generated method stub
}
};
urlCon.getResponseCode();
System.out.println("返回的状态码是:");
} catch (Exception e) {
e.printStackTrace();
}
我知道我很小白 求高手帮帮忙 指点下 该去查哪方面的资料 或者是给讲讲 谢谢 没分了 好桑心啊 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询