2012-11-20 15 views
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); 
    } 

} 

tôi đã sử dụng trên dode để phê chuẩn url,xác nhận URL hoạt động tốt nhưng giữ kết nối và hạn chế bất kỳ yêu cầu thêm

nếu ĐẦU cho tôi mã phản hồi 200 sau đó duy nhất tôi mở một url để webview.

nhưng một khi tôi gọi nó là giữ kết nối và không cho phép bất kỳ yêu cầu http nào khác ngay cả sau khi đóng, phải làm gì?

Safe use of HttpURLConnection

Trả lời

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

này đang làm việc như HttpURLConnection giữ kết nối sống và do đó chúng tôi cann't làm cho thực hiện yêu cầu hơn nữa để giữ-sống thiết lập để quyết sai vấn đề này.

1

Đối với đóng cửa đúng HttpURLConnection đi đến link

By URL này được đóng lại.

Các vấn đề liên quan