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

Java tutorial

Introduction

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

Source

package org.apache.commons.httpclient.demo;

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

/*
 2 GETPOST
    
 GETPOSTGETURL&http://java.sun.com?name=liudong&mobile=123456POST
    
*/

/**
 * 
 * 
 * 1330227
 * @author Liudong
 */

public class SimpleHttpClient {

    public static void main(String[] args) throws IOException {

        HttpClient client = new HttpClient();
        //
        //client.getHostConfiguration().setProxy("90.0.12.21",808);

        client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");
        HttpMethod method = getPostMethod(); //POST
        //HttpMethod method = getPostMethod(); //GET
        client.executeMethod(method);

        //
        System.out.println("===================================");
        System.out.println("");
        System.out.println(method.getStatusLine());
        System.out.println("===================================");

        //
        String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
        //
        System.out.println("===================================");
        System.out.println(":");
        System.out.println(response);
        System.out.println("===================================");

        method.releaseConnection();
    }

    /**
     * POST
     * @return
     */
    private static HttpMethod getPostMethod() {
        PostMethod post = new PostMethod("/search2005.php");
        NameValuePair simcard = new NameValuePair("searchkeyword", "1330227");
        post.setRequestBody(new NameValuePair[] { simcard });
        return post;
    }
}