5
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を検証するために上記のdodeを使用しました、

HEADが応答コード200をくれた場合、私だけがwebviewへのURLを開きます。

しかし、これを呼び出すと、接続を維持し、閉じた後でも他のhttpリクエストを許可しません。どうすればよいですか?

HttpURLConnectionの安全な使用

4

2 に答える 2

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

これは、HttpURLConnectionが接続を維持するために機能しているため、これ以上要求を行うことができないため、キープアライブをfalseに設定するとこの問題が解決します。

于 2012-11-20T11:03:47.270 に答える
1

正しく閉じるにはHttpURLConnectionリンクに移動します

このURLによって閉じられます。

于 2012-11-23T07:50:36.990 に答える