org.apache.http.client.HttpClient

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpClientTests {
public static void main(String[] args) throws IOException{
HttpClient client=new DefaultHttpClient();
//POST url
String posturl="http://localhost:8080/a.action";
//建立HttpPost对象
HttpPost httpPost=new HttpPost(posturl);
//建立一个NameValuePair数组,用于存储欲传递的参数
List params=new ArrayList();
//添加参数
params.add(new BasicNameValuePair("start", "0"));
params.add(new BasicNameValuePair("size", "14"));
params.add(new BasicNameValuePair("sortType", "hot"));
//设置编码
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//发送Post,并返回一个HttpResponse对象  
HttpResponse response=new DefaultHttpClient().execute(httpPost);
//可以得到指定的header
//         Header header = response.getFirstHeader("Content-Length");
// String Length=header.getValue(); 
// System.out.println(header.getName());
//如果状态码是200,则正常返回
if(response.getStatusLine().getStatusCode()==200){
//获得返回的字符串
String result=EntityUtils.toString(response.getEntity());
//打印输出
System.err.println(result);
//如果是下载的文件,可以用response.getEntity().getContent返回InputStream 
}
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值