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

HttpPost客户端请求实例

2014-03-05 17:25 441 查看
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String url="http://localhost/newspaper/test/1.php";
//POST的URL
HttpPost httppost=new HttpPost(url);
//建立HttpPost对象
List<NameValuePair> params=new ArrayList<NameValuePair>();
//建立一个NameValuePair数组,用于存储 传送的参数
params.add(new BasicNameValuePair("pwd","2544"));
//添加参数
httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//设置编码
HttpResponse response=new DefaultHttpClient().execute(httppost);
//发送Post,并返回一个HttpResponse对象
//Header header = response.getFirstHeader("Content-Length");
//String Length=header.getValue();
// 上面两行可以得到指定的Header
if(response.getStatusLine().getStatusCode()==200){//如果状态码为200,就是正常返回
String result=EntityUtils.toString(response.getEntity());
//得到返回的字符串
System.out.println(result);

//如果是下载文件,可以用response.getEntity().getContent()返回InputStream
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: