-1

嗨,在我的 android 应用程序中,我将 HttpUrlconnection 用于 Web 服务。因此,对于任何带有响应 401 的 Web 资源请求,它都不会提供任何标头字段或响应代码。它显示内容长度 -1。它会引发异常java.io.IOException: Received authentication challenge is null。现在我的问题是如何检查这种情况的响应代码。需要帮忙。我在休息客户端检查了这些请求,在那里我发现我的响应正确地带有带有 401 和其他字段的标头响应代码

我的请求看起来像

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。我做错了什么需要帮助。

4

2 回答 2

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()
于 2013-09-30T10:03:44.987 回答
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();
      }
于 2013-09-30T09:57:58.693 回答