Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26187401
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2010-04-25 12:27:01

HttpClient示例代码

[作者:遥方 时间:2010-4-25]

Demo1. GET一段URL并返回HTTP头部响应信息

package cn.ty.http;

import java.io.IOException;

import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.methods.*;

/**

 * 最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面

 * @author Liudong

 */

public class SimpleClient {

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

    {

        HttpClient client = new HttpClient();  

        //设置代理服务器地址和端口    

        //client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);

        //使用GET方法,如果服务器需要通过HTTPS连接,那只需要将下面URL中的http换成https

        HttpMethod method = new GetMethod("");

        //使用POST方法

        //HttpMethod method = new PostMethod("");

        client.executeMethod(method);

        //打印服务器返回的状态

        System.out.println(method.getStatusLine());

        Header[] header = method.getResponseHeaders();

        for (Header header2 : header) {

            System.out.println(header2);

        }        System.out.println(method.getResponseHeader("Content-Type").getValue());

        //打印返回的信息

        //System.out.println(method.getResponseBodyAsString());

        //释放连接

        method.releaseConnection();

    }

}

 

 

 

 

 

 

 

 

Demo2. 模拟登录

package cn.ty.http;

import java.io.IOException;

import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.cookie.CookiePolicy;

import org.apache.commons.httpclient.cookie.CookieSpec;

import org.apache.commons.httpclient.methods.*;

 

public class SimpleClient {

    static final String LOGON_SITE = "login.xxxx.xx";

    static final int    LOGON_PORT = 80;

 

    public SimpleClient() {

        super();

    }

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

    HttpClient client = new HttpClient();

        client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");

        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

        CookieSpec cookiespec = CookiePolicy.getDefaultSpec();

        Cookie[] initcookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());

        if (initcookies.length == 0) {

            System.out.println("None");   

        } else {

            for (int i = 0; i < initcookies.length; i++) {

                System.out.println("- " + initcookies[i].toString());   

            }

        }

        PostMethod authpost = new PostMethod("/user/xxxxxxxxx.asp");

        NameValuePair action   = new NameValuePair("forwardURL", "");

        NameValuePair url      = new NameValuePair("returnURL", "");

        NameValuePair userid   = new NameValuePair("vwriter", "xxxx");

        NameValuePair password = new NameValuePair("vpassword", "xxxxxx");

        authpost.setRequestBody(new NameValuePair[] {action, url, userid, password});

        client.executeMethod(authpost);

        System.out.println("Login form post: " + authpost.getStatusLine().toString());

        authpost.releaseConnection();

        //登录完了之后就可以查找看下是否有cookie

        Cookie[] logoncookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());

        System.out.println("登录之后的 cookies:");   

        if (logoncookies.length == 0) {

            System.out.println("None");   

        } else {

            for (int i = 0; i < logoncookies.length; i++) {

                System.out.println("- " + logoncookies[i].toString());   

            }

        }

        //处理重定向的情况即30X

        int statuscode = authpost.getStatusCode();

        if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||

            (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||

            (statuscode == HttpStatus.SC_SEE_OTHER) ||

            (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {

            Header header = authpost.getResponseHeader("location");

            if (header != null) {

                String newuri = header.getValue();

                if ((newuri == null) || (newuri.equals(""))) {

                    newuri = "/";

                }

                System.out.println("Redirect target: " + newuri);

                GetMethod redirect = new GetMethod(newuri);

                client.executeMethod(redirect);

                System.out.println("Redirect: " + redirect.getStatusLine().toString());

                // release any connection resources used by the method

                System.out.println(redirect.getResponseBodyAsString());

                redirect.releaseConnection();

            } else {

                System.out.println("Invalid redirect");

                System.exit(1);

            }

        }

    }

}

 

 

 

 

 

 

 

 

 

Demo3. 使用代理

package cn.ty.http;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.net.Socket;

 

import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.auth.AuthScope;

import org.apache.commons.httpclient.cookie.CookiePolicy;

import org.apache.commons.httpclient.cookie.CookieSpec;

import org.apache.commons.httpclient.methods.*;

 

public class SimpleClient {

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

        ProxyClient proxyclient = new ProxyClient();

        // set the host the proxy should create a connection to

        //

        // Note:  By default port 80 will be used. Some proxies only allow conections

        // to ports 443 and 8443.  This is because the HTTP CONNECT method was intented

        // to be used for tunneling HTTPS.

        proxyclient.getHostConfiguration().setHost("");

        // set the proxy host and port

        proxyclient.getHostConfiguration().setProxy("119.62.128.38", 80);

        // set the proxy credentials, only necessary for authenticating proxies

        proxyclient.getState().setProxyCredentials(

            new AuthScope("119.62.128.38", 80, null),

            new UsernamePasswordCredentials("proxy", "proxy"));

       

        // create the socket 连接到代理服务器

        ProxyClient.ConnectResponse response = proxyclient.connect();

        //表示连接成功了

        if (response.getSocket() != null) {

            Socket socket = response.getSocket();

            try {

                // go ahead and do an HTTP GET using the socket

              Writer out = new OutputStreamWriter(socket.getOutputStream(), "ISO-8859-1");

             

                out.write("POST   HTTP/1.1\r\n"); 

                out.write("Host: \r\n"); 

                out.write("\r\n"); 

                out.flush(); 

                BufferedReader in = new BufferedReader(

                    new InputStreamReader(socket.getInputStream(), "gbk"));

                String line = null;

                while ((line = in.readLine()) != null) {

                    System.out.println(line);

                }

            } finally {

                // be sure to close the socket when we're done

                socket.close();

            }

        } else {

            // the proxy connect was not successful, check connect method for reasons why

            System.out.println("Connect failed: " + response.getConnectMethod().getStatusLine());

            System.out.println(response.getConnectMethod().getResponseBodyAsString());

        }

    }

 

}

附:常用的代理服务器与端口列表

119.62.128.38:80 HTTP250,875,875 南省 联通

"222.169.11.234", 8080

先用这两个吧。有时间再上网找找看有没有免费的代理服务器

 

 

阅读(2316) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~