gpt4 book ai didi

android - URL 验证工作正常但保持连接并限制任何进一步的请求

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:38:37 25 4
gpt4 key购买 nike

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);
}

}

我使用上面的 dode 来验证 url,

如果 HEAD 给我响应代码 200,那么只有我打开一个 url 到 webview。

但是一旦我调用它,它就会保持连接并且即使在关闭后也不允许任何其他 http 请求,该怎么办?

Safe use of HttpURLConnection

最佳答案

huc.setRequestProperty("keep-alive", "false");

这是因为 HttpURLConnection 保持连接有效,因此我们无法发出进一步的请求,因此将 keep-alive 设置为 false 可以解决此问题。

关于android - URL 验证工作正常但保持连接并限制任何进一步的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13471333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com