HttpURLConnection超时设置

如果要用5秒钟以上的时间来连接URL,我想返回false-使用Java怎么可能?这是我用来检查URL是否有效的代码


HttpURLConnection.setFollowRedirects(false);

HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();

con.setRequestMethod("HEAD");

return (con.getResponseCode() == HttpURLConnection.HTTP_OK);


红糖糍粑
浏览 1665回答 3
3回答

繁花如伊

HttpURLConnection有一个setConnectTimeout方法。只需将超时设置为5000毫秒,然后捕获 java.net.SocketTimeoutException您的代码应如下所示:try {   HttpURLConnection.setFollowRedirects(false);   HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();   con.setRequestMethod("HEAD");   con.setConnectTimeout(5000); //set timeout to 5 seconds   return (con.getResponseCode() == HttpURLConnection.HTTP_OK);} catch (java.net.SocketTimeoutException e) {   return false;} catch (java.io.IOException e) {   return false;}

慕斯王

如果HTTP连接没有超时,则可以在后台线程本身(AsyncTask,Service等)中实现超时检查器,以下类是Customize AsyncTask的示例,该示例在特定时间段后超时public abstract class AsyncTaskWithTimer<Params, Progress, Result> extends&nbsp; &nbsp; AsyncTask<Params, Progress, Result> {private static final int HTTP_REQUEST_TIMEOUT = 30000;@Overrideprotected Result doInBackground(Params... params) {&nbsp; &nbsp; createTimeoutListener();&nbsp; &nbsp; return doInBackgroundImpl(params);}private void createTimeoutListener() {&nbsp; &nbsp; Thread timeout = new Thread() {&nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Looper.prepare();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Handler handler = new Handler();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler.postDelayed(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (AsyncTaskWithTimer.this != null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && AsyncTaskWithTimer.this.getStatus() != Status.FINISHED)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AsyncTaskWithTimer.this.cancel(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler.removeCallbacks(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Looper.myLooper().quit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, HTTP_REQUEST_TIMEOUT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Looper.loop();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; timeout.start();}abstract protected Result doInBackgroundImpl(Params... params);}一个样品public class AsyncTaskWithTimerSample extends AsyncTaskWithTimer<Void, Void, Void> {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCancelled(Void void) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "Async Task onCancelled With Result");&nbsp; &nbsp; &nbsp; &nbsp; super.onCancelled(result);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCancelled() {&nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "Async Task onCancelled");&nbsp; &nbsp; &nbsp; &nbsp; super.onCancelled();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected Void doInBackgroundImpl(Void... params) {&nbsp; &nbsp; &nbsp; &nbsp; // Do background work&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; };&nbsp;}

呼唤远方

我可以通过添加简单的行来获得针对此类类似问题的解决方案HttpURLConnection hConn = (HttpURLConnection) url.openConnection();hConn.setRequestMethod("HEAD");我的要求是知道响应代码,为此,仅获取元信息就足够了,而不是获取完整的响应正文。默认的请求方法是GET,要花很多时间才能返回,最后抛出了SocketTimeoutException。当我将Request Method设置为HEAD时,响应速度非常快。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java