URL验证工作正常,但保持连接,并限制任何进一步的请求
public class UrlVarificationTask extends AsyncTask<Void, Void, Integer> { private String url;
private UrlVarificationDelegate delegate;
private HttpURLConnection huc;
public UrlVarificationTask(String url, UrlVarificationDelegate delegate) {
this.url = url;
this.delegate = delegate;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Integer doInBackground(Void... params) {
int responseCode = 0;
try {
System.setProperty("http.keepAlive", "false");
URL u = new URL(url);
huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("HEAD");
huc.connect();
responseCode = huc.getResponseCode();
} catch (MalformedURLException mal) {
responseCode = -555;
} catch (IOException io) {
responseCode = -444;
} catch (Exception ex) {
responseCode = -333;
ex.printStackTrace();
}
if (huc != null) {
try {
huc.getInputStream().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
huc.disconnect();
}
Logger.debug("Response Code==" + responseCode);
return responseCode;
}
@Override
protected void onPostExecute(Integer responseCode) {
delegate.urlVarificationResponseCode(responseCode);
super.onPostExecute(responseCode);
}
}
我上面核桃用于验证网址,URL验证工作正常,但保持连接,并限制任何进一步的请求
如果HEAD给我响应代码200只,然后我打开一个URL网页视图。
但是一旦我调用它,它保持连接并且不允许任何其他的http请求,即使关闭后,该怎么办?
Safe use of HttpURLConnection
回答:
huc.setRequestProperty("keep-alive", "false");
这是工作为HttpURLConnection的保持连接,因此我们水湿让做进一步的请求,从而保持活动设置为false解决这个问题。
回答:
对于正常关闭HttpURLConnection
转到link
通过这个URL被关闭。
以上是 URL验证工作正常,但保持连接,并限制任何进一步的请求 的全部内容, 来源链接: utcz.com/qa/264844.html