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();
}
* 使用POST方式提交数据
* @return
*/
private static HttpMethod getPostMethod(){
PostMethod post = new PostMethod( "/search2005.php ");
NameValuePair searchkeyword = new NameValuePair( "searchkeyword ", "1330227 ");
post.setRequestBody(new NameValuePair[] { searchkeyword});
return post;
}
}
编译通过,执行的时候出现的信息如下:
---------- java ----------
2007-9-10 11:48:37 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
信息: I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect
2007-9-10 11:48:37 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
信息: Retrying request
2007-9-10 11:48:58 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
信息: I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect