org.apache.commons.httpclient.demo.GetSample.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.commons.httpclient.demo.GetSample.java

Source

package org.apache.commons.httpclient.demo;

import java.io.*;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

//ApacheHttpClienthttp

/*
 HttpClient  Apache Jakarta Common HttpClient HttpClient Apache 
    
 3jar
 1commons-httpclient-3.0.1.jar
 2commons-logging-1.1.jar
 3commons-codec-1.3.jar
    
 Test_baidu.html

*/

public class GetSample {
    public static void main(String[] args) {
        //HttpClient
        HttpClient httpClient = new HttpClient();
        //
        //httpClient.getHostConfiguration().setProxy("90.0.12.21",808);
        //GET
        GetMethod getMethod = new GetMethod("http://www.baidu.com");
        //GetMethod getMethod = new GetMethod("http://10.164.80.52/dav/5000/moban.rar");
        //
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        try {
            //getMethod
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + getMethod.getStatusLine());
            }
            //
            byte[] responseBody = getMethod.getResponseBody();

            String serverfile = "Test_baidu.html";
            //String serverfile = "d:\\moban.rar";
            OutputStream serverout = new FileOutputStream(serverfile);

            serverout.write(responseBody);
            serverout.flush();
            serverout.close();

            //
            //System.out.println(new String(responseBody));
            System.out.println("OK!");
        } catch (HttpException e) {
            //
            System.out.println("Please check your provided http address!");
            e.printStackTrace();
        } catch (IOException e) {
            //
            e.printStackTrace();
        } finally {
            //
            getMethod.releaseConnection();
        }
    }
}