您的位置:首页 > 理论基础 > 计算机网络

java httpclient发送post get 请求

2011-04-22 09:59 696 查看
import java.io.IOException;

import org.apache.commons.httpclient.*;

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

public class SimpleHttpClient {

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

{

HttpClient client = new HttpClient();

int i =0;

while( i<1000){

client.getHostConfiguration().setHost("60.217.249.98", 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();

i++;

}

}

/**

* 使用GET方式提交数据

* @return

*/

/* private static HttpMethod getGetMethod(){

return new GetMethod(" ");

}*/

/**

* 使用POST方式提交数据

* @return

*/

private static HttpMethod getPostMethod(){

PostMethod post = new PostMethod("/content/refresh");

NameValuePair username = new NameValuePair("username","chinacache");

NameValuePair password = new NameValuePair("password","chinacache");

NameValuePair task = new NameValuePair("task","{urls:[/"http://test.gamedl.qq.chinacache.net/"]}");

post.setRequestBody(new NameValuePair[] { username,password,task});

return post;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: