2009年7月3日金曜日

[Java]how to use the gzipped http response by using the apache http-client ver.3.x

how to use the gzipped http response by using the apache http-client ver.3.x

  • References
  • Example
    //set url
    String url="http://hc.apache.org/httpclient-3.x/";

    //initialize apache HttpClient object and HttpMethod
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    //set followRedirects function to true if you need.
    httpMethod.setFollowRedirects(true);
    //set Accept-Encoding request header
    httpMethod.setRequestHeader("Accept-Encoding","gzip");

    try{
    //access to the url and get response status
    int status = httpClient.executeMethod(http_method);

    //check response status. if the value of response status is set to 200, get body stream
    if(status == 200){
    //check the response header "Content-Encoding". the value of Content-Encoding header contains the
    //"gzip" value, this means the response stream is gzipped.
    Header contentEncodingHeader=httpMethod.getResponseHeader("Content-Encoding");
    String contentEncoding = contentEncodingHeader!=null ? contentEncodingHeader.getValue() : "";
    String contentEncodingLowerCase=contentEncoding.toLowerCase();
    boolean isGzipped=(contentEncodingLowerCase.indexOf("gzip")>=0);

    //get response stream
    InputStream stream=httpMethod.getResponseBodyAsStream();

    InputStream bodyStream=null;
    ByteArrayOutputStream outStream=null;
    try{
    //if the response stream is gzipped, derived stream is converted into the GZIPInputStream.
    //the response stream is not gzipped, set the stream without conversion
    bodyStream = isGzipped ? new GZIPInputStream(stream) : stream;

    //change the response from InputStream to Byte Array.
    outStream=new ByteArrayOutputStream();
    byte[] buffer = new byte[4096];
    int length;
    while((length=bodyStream.read(buffer))>0){
    outStream.write(buffer,0,length);
    }

    //get the response charset.
    String charset=httpMethod.getResponseCharSet();
    //convert the response byte array to the String object.
    String body=new String(outStream.toByteArray(),charset);

    //Instead of converting the InputStream to Byte array,
    //we can convert the inputstream to String by using the InputStreamReader and BufferedReader directly.
    //But we can't read the responsed InputStream twice.
    //for example.
    //InputStreamReader bodyReader=new InputStreamReader(bodyStream,charset);
    //BufferedReader bodyBufferedReader=new BufferedReader(bodyReader);
    //String line=bodyBufferedReader.readLine();
    //while(line!=null){
    // bodyBuffer.append(line);
    // line=bodyBufferedReader.readLine();
    //}
    //String body=bodyBuffer.toString();
    }catch(Exception e1){
    throw e1;
    }finally{
    //close ByteArrayOutputStream
    if(outStream!=null){
    try{
    outStream.close();
    }catch(Exception ignore){}
    }

    //close InputStream
    if(bodyStream!=null){
    try{
    bodyStream.close();
    }catch(Exception ignore){}
    }
    if(stream!=null){
    try{
    stream.close();
    }catch(Exception ignore){}
    }
    }
    }
    }catch(Exception e0){
    System.out.println("Error, "+e0);
    }finally{
    httpMethod.releaseConnection();
    }


0 件のコメント: