2012-04-05 8 views
-2

Javaコードを使用してサーバー応答コードを特定するにはどうすればよいですか。サーバーからHTTPレスポンスとして何らかの応答があった場合、これを文字列などで印刷できるはずです。また、いくつかの特定のリクエストがサーバーにヒットし、サーバー応答コードも見つかった場合、どのようにJavaで追跡するのか知りたかったのです。Javaを使用してサーバー応答コードを見つける方法

+0

HttpClientまたはURLConnectionを使用しているか、独自の実装を使用しているかのように、具体的にしたいですか? – ahanin

+0

申し訳ありませんが、要求を行うためにHttpClientを使用しています – aditi

+0

次に、ステータスコードを取得するためにHttpResponse.getStatusLine()。getStatusCode()を呼び出すことができます:http://hc.apache.org/httpcomponents-core-ga/httpcore/ apidocs/org/apache/http/HttpResponse.html – ahanin

答えて

2
Javaでは

とは、HTTPヘッダコードにアクセスしたい場合には、あなたがこれを使用することができます:

URL url = new URL("http://www.google.com"); 
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection(); 
openConnection.connect(); 
int rCode = openConnection.getResponseCode()); 
0

あなたは、このコードのヘルプのURLConnectionを使用する場合は、あなたとあなたのようにHTTPレスポンスを印刷することができます文字列。 http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpResponse.html

をそして、あなたはHttpResponse.getEntity()を呼び出し、応答エンティティを取得するための:

String output = null; 
output = getHttpResponseRabby("input url link"); 

public static String getHttpResponseRabby(String location) { 

    String result = ""; 
    URL url = null; 
    Log.d("http:", "balance information"); 

    try { 
     url = new URL(location); 
     Log.d("http:", "URL Link" + url); 
    } catch (MalformedURLException e) { 
     Log.e("http:", "URL Not found" + e.getMessage()); 
    } 

    if (url != null) { 
     try { 
      BufferedReader in; 
      HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
      urlConn.setConnectTimeout(1000); 
      while(true) 
      { 
       try 
        { 
        in = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 
        } 
       catch (IOException e) 
        { 
         break; 
        } 


       String inputLine; 

       int lineCount = 0; // limit the lines for the example 
       while ((lineCount < 5) && ((inputLine = in.readLine()) != null)) 
        { 
         lineCount++; 

         result += inputLine; 
        } 

       in.close(); 
       urlConn.disconnect(); 
       return result; 
      } 
     } catch (IOException e) { 
      Log.e("http:", "Retrive data" + e.getMessage()); 
     } 
    } else { 
     Log.e("http:", "FAILED TO RETRIVE DATA" + " url NULL"); 
    } 
    return result; 
} 
関連する問題