快乐着飞舞着

---Nothing to do is doing nothing
随笔 - 93, 文章 - 5, 评论 - 56, 引用 - 0
数据加载中……

应用HttpClient来对付各种顽固的WEB服务器(2) [转]

2. 以GET或者POST方式向网页提交参数

其实前面一个最简单的示例中我们已经介绍了如何使用GET或者POST方式来请求一个页面,本小节与之不同的是多了提交时设定页面所需的参数,我们知道如果是GET的请求方式,那么所有参数都直接放到页面的URL后面用问号与页面地址隔开,每个参数用&隔开,例如:http://java.sun.com?name=liudong&mobile=123456,但是当使用POST方法时就会稍微有一点点麻烦。本小节的例子演示向如何查询手机号码所在的城市,代码如下:

 

/*

 * Created on 2003-12-7 by Liudong

 */

package http.demo;

 

import java.io.IOException;

 

import org.apache.commons.httpclient.*;

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

/**

 * 提交参数演示

 * 该程序连接到一个用于查询手机号码所属地的页面

 * 以便查询号码段1330227所在的省份以及城市

 * @author Liudong

 */

public class SimpleHttpClient {

 

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

    {

        HttpClient client = new HttpClient();

        client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");

 

        HttpMethod method = getPostMethod();//使用POST方式提交数据

        client.executeMethod(method);

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

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

        //打印结果页面

        String response =

           new String(method.getResponseBodyAsString().getBytes("8859_1"));

       //打印返回的信息

        System.out.println(response);

        method.releaseConnection();

    }

    /**

     * 使用GET方式提交数据

     * @return

     */

    private static HttpMethod getGetMethod(){

        return new GetMethod("/simcard.php?simcard=1330227");

    }

    /**

     * 使用POST方式提交数据

     * @return

     */

    private static HttpMethod getPostMethod(){

        PostMethod post = new PostMethod("/simcard.php");

        NameValuePair simcard = new NameValuePair("simcard","1330227");

        post.setRequestBody(new NameValuePair[] { simcard});

        return post;

    }

}

在上面的例子中页面http://www.imobile.com.cn/simcard.php需要一个参数是simcard,这个参数值为手机号码段,即手机号码的前七位,服务器会返回提交的手机号码对应的省份、城市以及其他详细信息。GET的提交方法只需要在URL后加入参数信息,而POST则需要通过NameValuePair类来设置参数名称和它所对应的值

3. 处理页面重定向

在JSP/Servlet编程中response.sendRedirect方法就是使用HTTP协议中的重定向机制。它与JSP中的<jsp:forward …>的区别在于后者是在服务器中实现页面的跳转,也就是说应用容器加载了所要跳转的页面的内容并返回给客户端;而前者是返回一个状态码,这些状态码的可能值见下表,然后客户端读取需要跳转到的页面的URL并重新加载新的页面。就是这样一个过程,所以我们编程的时候就要通过HttpMethod.getStatusCode()方法判断返回值是否为下表中的某个值来判断是否需要跳转。如果已经确认需要进行页面跳转了,那么可以通过读取HTTP头中的location属性来获取新的地址。

状态码
 对应HttpServletResponse的常量
 详细描述
 
301
 SC_MOVED_PERMANENTLY
 页面已经永久移到另外一个新地址
 
302
 SC_MOVED_TEMPORARILY
 页面暂时移动到另外一个新的地址
 
303
 SC_SEE_OTHER
 客户端请求的地址必须通过另外的URL来访问
 
307
 SC_TEMPORARY_REDIRECT
 同SC_MOVED_TEMPORARILY
 


下面的代码片段演示如何处理页面的重定向

client.executeMethod(post);

        System.out.println(post.getStatusLine().toString());

        post.releaseConnection();

       

        //检查是否重定向

        int statuscode = post.getStatusCode();

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

            (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||

            (statuscode == HttpStatus.SC_SEE_OTHER) ||

(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {

//读取新的URL地址

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

            if (header != null) {

                String newuri = header.getValue();

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

                    newuri = "/";

                GetMethod redirect = new GetMethod(newuri);

                client.executeMethod(redirect);

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

                redirect.releaseConnection();

            } else

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

        }

我们可以自行编写两个JSP页面,其中一个页面用response.sendRedirect方法重定向到另外一个页面用来测试上面的例子。

(浏览全文  引用此文)
文章来源:http://www.blogcn.com/user28/reene/blog/22216676.html

posted on 2005-10-09 13:07 快乐着飞舞着 阅读(3681) 评论(5)  编辑 收藏 引用 所属分类: Java

评论

# re: 应用HttpClient来对付各种顽固的WEB服务器(2) [转]  回复  更多评论   

兄弟,你说的方法不行哈,我试了post的方式提交参数,不行的,返回没有任何值
2006-09-18 15:01 | 江南

# re: 应用HttpClient来对付各种顽固的WEB服务器(2) [转]  回复  更多评论   

这篇文章是我转载的,但是我用过httpClient,用post方法没有问题。
请仔细检查代码或阅读apache的文档。~~

2006-09-18 22:02 | 快乐着飞舞着

# re: 应用HttpClient来对付各种顽固的WEB服务器(2) [转]  回复  更多评论   

兄弟,好像不行啊
我要获取这个网页的内容,需要登陆的,我有用户名和密码,我的程序如下:
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class SimpleHttpClient {

public static void main(String[] args) throws IOException
{
HttpClient client = new HttpClient();
//client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");
client.getHostConfiguration().setHost("www.musicool.cn",80,"http");
HttpMethod method = getPostMethod();//使用POST方式提交数据
//HttpMethod method = getGetMethod();
client.executeMethod(method);
//打印服务器返回的状态
System.out.println(method.getStatusLine());
//打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
//打印返回的信息
System.out.println(response);
method.releaseConnection();
}
/**
* 使用GET方式提交数据
* @return
*/
private static HttpMethod getGetMethod(){
return new GetMethod("/bbs/thread-46688-1-1.html?loginfield=renmiaogen&password=8609221027&questionid=0");
}
/**
* 使用POST方式提交数据
* @return
*/
private static HttpMethod getPostMethod(){
//PostMethod post = new PostMethod("/simcard.php");
PostMethod post = new PostMethod("/bbs/thread-46688-1-1.html");
//NameValuePair simcard = new NameValuePair("simcard","1330227");
NameValuePair userid = new NameValuePair("loginfield","renmiaogen");
NameValuePair password = new NameValuePair("password","8609221027");
NameValuePair questionid = new NameValuePair("questionid","0");
post.setRequestBody(new NameValuePair[] { userid,password,questionid });
return post;
}
}
但我得到的内容还是没有登陆的内容,提醒我要登陆.
你帮你我看看是怎么回事?急!
2006-09-29 10:00 | renmiaogen

# re: 应用HttpClient来对付各种顽固的WEB服务器(2) [转]  回复  更多评论   

这个还是我以前用的, 难道现在有更新了?
建议参考apache的文档

同时 你可以使用httplook之类的工具 看http的数据传送是不是正常
2006-09-30 10:37 | 快乐着飞舞着

# re: 应用HttpClient来对付各种顽固的WEB服务器(2) [转]  回复  更多评论   

??
2010-01-05 13:52 | ??
只有注册用户登录后才能发表评论。

Clicky