-1

嗨在我的Android應用程序我使用HttpUrlconnection的Web服務。因此,對於任何具有響應401的Web資源請求,它不會給出任何標題字段或響應代碼。並且它顯示內容長度爲-1。它引發異常java.io.IOException: Received authentication challenge is null。現在我的問題是如何檢查這些條件的響應代碼。需要幫忙。我檢查在休息客戶的請求,但我發現,我的迴應與包頭響應碼來正確地與401等領域也無法獲得響應代碼爲401與HTTP網址連接

我的要求看起來像

String urlString ="http://WWW.ABC.com"; 
URL url = new URL(urlString); 
login_urlConnection = (HttpURLConnection) url.openConnection(); 
login_urlConnection.setRequestMethod("GET"); 
int statusCode = login_urlConnection.getResponseCode() // Here I am getting exception I want this status code to check authentication failure 

如何來解決這個問題。需要幫忙。謝謝。

我做了與HttpClient相同的調用,然後我能夠得到響應代碼,那麼爲什麼不用HttpUrlConnection。我做錯了什麼需要幫助。

+0

http://android-spirit.blogspot.in/2013/07/cosume-phpget-method-webservice-in.html – Nirmal

回答

0
URL url = new URL("http://www.android.com/"); 
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    try { 
    InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
    read(in); 
    finally { 
    urlConnection.disconnect(); 
    } 

閱讀使用此塊:

int i; 
     char c; 

     try{ 
     // new input stream created 
     is = new FileInputStream("C://test.txt"); 

     System.out.println("Characters printed:"); 

     // reads till the end of the stream 
     while((i=is.read())!=-1) 
     { 
      // converts integer to character 
      c=(char)i; 

      // prints character 
      System.out.print(c); 
     } 
     }catch(Exception e){ 

     // if any I/O error occurs 
     e.printStackTrace(); 
     }finally{ 

     // releases system resources associated with this stream 
     if(is!=null) 
      is.close(); 
     } 
+0

我的代碼是針對沒有發生認證失敗,而其他請求正常工作。意思是我無法獲得那些發生認證失敗的請求的代碼和頭文件。 – nilkash

0

urlString應改爲 「http://WWW.ABC.com」。 Url Scheme(如http://或ftp://等)是必需的。

此外,您需要連接到服務器。

login_urlConnection.setRequestMethod("GET"); 
try { 
    login_urlConnection.connect(); 
} catch (IOException e) { 
    e.printstackTrace(); 
} 
int statusCode = login_urlConnection.getResponseCode() 
+0

謝謝你的迴應。我試過你是解決方案。但它仍然是同樣的問題。有沒有其他解決方案。只有當身份驗證失敗時,它纔會給出200或其他狀態碼的正確狀態碼,它會使用-1和無狀態碼提供長度爲givinf的頭部長度。 – nilkash

相關問題