-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

相关问题