package com.test;
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;
/**
* @author Administrator
*
*/
public class SampleHttpClient {
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 方法提数据
HttpMethod method = getGetMethod();
client.executeMethod(method);
//打印服务器返回数据
System.out.println(method.getStatusLine());
String enStr = new String(method.getResponseBodyAsString()
.getBytes("ISO-8859-1"), "GB2312");
//打印页面结果
System.out.println(enStr);
method.releaseConnection();
}
/**
* 使用Get 方法提交数据
*/
private static HttpMethod getGetMethod (){
return new GetMethod("/simcard.php?simcard=1330227");
}
private static HttpMethod getPostMethod(){
PostMethod post = new PostMethod("/simcard.php");
NameValuePair simcard = new NameValuePair("simcard", "1330227");
post.setRequestBody(new NameValuePair[]{simcard});
return post;
}
}