Chinaunix
标题:
大家代码里都怎么发起http请求的~求深入
[打印本页]
作者:
ladyABC
时间:
2012-09-11 23:46
标题:
大家代码里都怎么发起http请求的~求深入
写了一个最简单的demo,用来发起http请求调用。
大家代码里都怎么个搞法?
求深入,求扩展~~
public static void main(String[] args) throws HttpException, IOException {
testHttp(0);
testHttp(1);
}
/**
* type=0, get
* type=1, post
* http://haoma.imobile.com.cn/index.php?mob=18858111994
*/
public static void testHttp(int type) throws HttpException, IOException{
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("haoma.imobile.com.cn", 80, "http");
HttpMethod method = null;
switch(type){
case 0:
method = getGetMethod();
break;
case 1:
method = getPostMethod();
break;
}
client.executeMethod(method);
System.out.println("statusLine = " + method.getStatusLine());
String response = new String(method.getResponseBodyAsString());
System.out.println("response = " + response);
method.releaseConnection();
}
private static HttpMethod getGetMethod() {
return new GetMethod("/index.php?mob=18858111994");
}
private static HttpMethod getPostMethod() {
PostMethod post = new PostMethod("/index.php");
NameValuePair simcard = new NameValuePair("mob", "18858111994");
post.setRequestBody(new NameValuePair[] { simcard });
return post;
}
复制代码
作者:
lloydm
时间:
2012-09-16 15:23
提示:
作者被禁止或删除 内容自动屏蔽
作者:
device_memory
时间:
2012-09-25 10:48
swt
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2