HttpPost客户端请求实例

来源:互联网 发布:android实时更新数据 编辑:程序博客网 时间:2024/06/07 08:52
public static void main(String[] args) throws Exception {// TODO Auto-generated method stubString url="http://localhost/newspaper/test/1.php";//POST的URLHttpPost 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();                // 上面两行可以得到指定的Headerif(response.getStatusLine().getStatusCode()==200){//如果状态码为200,就是正常返回String result=EntityUtils.toString(response.getEntity());//得到返回的字符串System.out.println(result);                       //如果是下载文件,可以用response.getEntity().getContent()返回InputStream}}

0 0
原创粉丝点击