java 用 HttpClients 请求 url 返回状态码是 503 怎么处理

 我来答
裔婉君
2016-09-26 · 超过70用户采纳过TA的回答
知道小有建树答主
回答量:330
采纳率:5%
帮助的人:129万
展开全部
// 用getResponseCode可以获取<a href="https://www.baidu.com/s?wd=URL&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9rHnkmWD3nWbdnANBnj-W0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHRYnHndP1fdPH6Ynj0snWfzPs" target="_blank" class="baidu-highlight">URL</a>返回状态码
String s<a href="https://www.baidu.com/s?wd=url&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9rHnkmWD3nWbdnANBnj-W0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHRYnHndP1fdPH6Ynj0snWfzPs" target="_blank" class="baidu-highlight">url</a> = "";
try {
s<a href="https://www.baidu.com/s?wd=url&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9rHnkmWD3nWbdnANBnj-W0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHRYnHndP1fdPH6Ynj0snWfzPs" target="_blank" class="baidu-highlight">url</a>="你的url";
URL url = new URL(surl);
URLConnection rulConnection = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;
httpUrlConnection.setConnectTimeout(300000);
httpUrlConnection.setReadTimeout(300000);
httpUrlConnection.connect();
String code = new Integer(httpUrlConnection.getResponseCode()).toString();
String message = httpUrlConnection.getResponseMessage();
System.out.println("getResponseCode code ="+ code);
System.out.println("getResponseMessage message ="+ message);
if(!code.startsWith("2")){
throw new Exception("ResponseCode is not begin with 2,code="+code);
}
System.out.println(getCurDateTime()+"连接"+surl+"正常");
}catch(Exception ex){
System.out.println(ex.getMessage());
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式