需求:用户登录(name:用户名,pwd:密码)

(一)HttpGet :doGet()方法
//doGet():将参数的键值对附加在url后面来传递

[java] view plain copy
  1. publicStringgetResultForHttpGet(Stringname,Stringpwd)throwsClientProtocolException,IOException{
  2. //服务器:服务器项目:servlet名称
  3. Stringpath="http://192.168.5.21:8080/test/test";
  4. Stringuri=path+"?name="+name+"&pwd="+pwd;
  5. //name:服务器端的用户名,pwd:服务器端的密码
  6. //注意字符串连接时不能带空格
  7. Stringresult="";
  8. HttpGethttpGet=newHttpGet(uri);//编者按:与HttpPost区别所在,这里是将参数在地址中传递
  9. HttpResponseresponse=newDefaultHttpClient().execute(httpGet);
  10. if(response.getStatusLine().getStatusCode()==200){
  11. HttpEntityentity=response.getEntity();
  12. result=EntityUtils.toString(entity,HTTP.UTF_8);
  13. }
  14. returnresult;
  15. }

(二)HttpPost :doPost()方法
//doPost():将参数打包到http报头中传递

[java] view plain copy
  1. publicStringgetReultForHttpPost(Stringname,Stringpwd)throwsClientProtocolException,IOException{
  2. //服务器:服务器项目:servlet名称
  3. Stringpath="http://192.168.5.21:8080/test/test";
  4. HttpPosthttpPost=newHttpPost(path);
  5. List<NameValuePair>list=newArrayList<NameValuePair>();
  6. list.add(newBasicNameValuePair("name",name));
  7. list.add(newBasicNameValuePair("pwd",pwd));
  8. httpPost.setEntity(newUrlEncodedFormEntity(list,HTTP.UTF_8));//编者按:与HttpGet区别所在,这里是将参数用List传递
  9. Stringresult="";
  10. HttpResponseresponse=newDefaultHttpClient().execute(httpPost);
  11. if(response.getStatusLine().getStatusCode()==200){
  12. HttpEntityentity=response.getEntity();
  13. result=EntityUtils.toString(entity,HTTP.UTF_8);
  14. }
  15. returnresult;
  16. }

-------------------------------------------------------------------------------------------------------

由此我们可知,HttpGet和HttpPost的区别在于前者是将参数在地址中传递,后者是将参数用List传递。

更多相关文章

  1. 万字长文带你了解最常用的开源 Squid 代理服务器
  2. Nginx系列教程(一)| 手把手教你在Linux环境下搭建Nginx服务
  3. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  4. android 接收SDCcard插拔的广播
  5. Android下得到APK包含信息
  6. android Post Get(普通)
  7. android中的layoutparams参数使用的简单总结
  8. build.gradle配置参数详解
  9. Android中MQTT的使用

随机推荐

  1. 简单了解MySQL数据库优化技巧
  2. 浅谈mysql通配符进行模糊查询的实现方法
  3. MySQL的视图和索引用法与区别详解
  4. mysql 8.0.21 安装配置方法图文教程
  5. MySQL索引优化Explain详解
  6. 浅谈MySQL使用笛卡尔积原理进行多表查询
  7. 数据库索引的知识点整理小结,你所需要了
  8. 详解MySQL监控工具 mysql-monitor
  9. 解决MySQL数据库意外崩溃导致表数据文件
  10. 快速了解MySQL 索引