HttpGet和Httppost的区别

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

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

  1. public String getResultForHttpGet(String name,String pwd) throws ClientProtocolException, IOException{
  2. //服务器 :服务器项目 :servlet名称
  3. String path="http://192.168.5.21:8080/test/test";
  4. String uri=path+"?name="+name+"&pwd="+pwd;
  5. //name:服务器端的用户名,pwd:服务器端的密码
  6. //注意字符串连接时不能带空格
  7. String result="";
  8. HttpGet httpGet=new HttpGet(uri);//编者按:与HttpPost区别所在,这里是将参数在地址中传递
  9. HttpResponse response=new DefaultHttpClient().execute(httpGet);
  10. if(response.getStatusLine().getStatusCode()==200){
  11. HttpEntity entity=response.getEntity();
  12. result=EntityUtils.toString(entity, HTTP.UTF_8);
  13. }
  14. return result;
  15. }
public String getResultForHttpGet(String name,String pwd) throws ClientProtocolException, IOException{ //服务器 :服务器项目 :servlet名称 String path="http://192.168.5.21:8080/test/test"; String uri=path+"?name="+name+"&pwd="+pwd; //name:服务器端的用户名,pwd:服务器端的密码 //注意字符串连接时不能带空格 String result=""; HttpGet httpGet=new HttpGet(uri);//编者按:与HttpPost区别所在,这里是将参数在地址中传递 HttpResponse response=new DefaultHttpClient().execute(httpGet); if(response.getStatusLine().getStatusCode()==200){ HttpEntity entity=response.getEntity(); result=EntityUtils.toString(entity, HTTP.UTF_8); } return result; }

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

  1. public String getReultForHttpPost(String name,String pwd) throws ClientProtocolException, IOException{
  2. //服务器 :服务器项目 :servlet名称
  3. String path="http://192.168.5.21:8080/test/test";
  4. HttpPost httpPost=new HttpPost(path);
  5. List<NameValuePair>list=new ArrayList<NameValuePair>();
  6. list.add(new BasicNameValuePair("name", name));
  7. list.add(new BasicNameValuePair("pwd", pwd));
  8. httpPost.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));//编者按:与HttpGet区别所在,这里是将参数用List传递
  9. String result="";
  10. HttpResponse response=new DefaultHttpClient().execute(httpPost);
  11. if(response.getStatusLine().getStatusCode()==200){
  12. HttpEntity entity=response.getEntity();
  13. result=EntityUtils.toString(entity, HTTP.UTF_8);
  14. }
  15. return result;
  16. }
public String getReultForHttpPost(String name,String pwd) throws ClientProtocolException, IOException{ //服务器 :服务器项目 :servlet名称 String path="http://192.168.5.21:8080/test/test"; HttpPost httpPost=new HttpPost(path); List<NameValuePair>list=new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("name", name)); list.add(new BasicNameValuePair("pwd", pwd)); httpPost.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));//编者按:与HttpGet区别所在,这里是将参数用List传递 String result=""; HttpResponse response=new DefaultHttpClient().execute(httpPost); if(response.getStatusLine().getStatusCode()==200){ HttpEntity entity=response.getEntity(); result=EntityUtils.toString(entity, HTTP.UTF_8); } return result; }

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

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

转载于:https://my.oschina.net/haquanwen/blog/56391

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值