有段时间没写博客了 今天带来一篇关于HttpClient接口的使用
以下列出的是 HttpClient 提供的主要的功能,要知道更多详细的功能可以参见 HttpClient 的主页。
- 实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
- 支持自动转向
- 支持 HTTPS 协议
- 支持代理服务器等
//创建http请求 HttpClient client=new DefaultHttpClient(); //创建连接得方式 如get post put 等 。。。 HttpPost post=new HttpPost("http://127.0.0.1:8080/User/user/test.htm"); // 创建参数队列 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("userName", "hubiao")); UrlEncodedFormEntity uefEntity; try { //设置参数的编码 uefEntity=new UrlEncodedFormEntity(formparams,"UTF-8"); //将参数封装到post请求中 post.setEntity(uefEntity); //根据我们创建的连接和请求的方式 开始发送请求 并得到响应的信息。 HttpResponse response=client.execute(post); //将响应的信息转换成httpEntity HttpEntity entity=response.getEntity(); //将httpEntity转换成String字符串 Sy