get和post的主要区别:
1. get是从服务器上获取数据,post是向服务器传送数据。
2. get是把参数数据队列加到提交表单的action属性所指的url中,值和表单内各个字段一一对应,在url中可以看到。post是通过http post机制,将表单内各个字段与其内容放置在html header内一起传送到action属性所指的url地址。用户看不到这个过程。
3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
4. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
5. get安全性非常低,post安全性较高。但是执行效率却比Post方法好。
原文地址:http://blog.csdn.net/heng615975867/article/details/9061339
GET传输:直接把要传输参数加到url连接中;
</pre><pre class="html" name="code">public String getResultForHttpGet(String name,String pwd) throws ClientProtocolException, IOException{
02. //服务器 :服务器项目 :servlet名称
03. String path="http://192.168.5.21:8080/test/test";
04. String uri=path+"?name="+name+"&pwd="+pwd;
05. //name:服务器端的用户名,pwd:服务器端的密码
06. //注意字符串连接时不能带空格
07.
08. String result="";
09.
10. HttpGet httpGet=new HttpGet(uri);//编者按:与HttpPost区别所在,这里是将参数在地址中传递
11. HttpResponse response=new DefaultHttpClient().execute(httpGet);
12. if(response.getStatusLine().getStatusCode()==200){
13. HttpEntity entity=response.getEntity();
14. result=EntityUtils.toString(entity, HTTP.UTF_8);
15. }
16. return result;
17. }
POST传输:需要把传输的参数打包到http报头中传递
public String getReultForHttpPost(String name,String pwd) throws ClientProtocolException, IOException{
02. //服务器 :服务器项目 :servlet名称
03. String path="http://192.168.5.21:8080/test/test";
04. HttpPost httpPost=new HttpPost(path);
05. List<NameValuePair>list=new ArrayList<NameValuePair>();
06. list.add(new BasicNameValuePair("name", name));
07. list.add(new BasicNameValuePair("pwd", pwd));
08. httpPost.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));//编者按:与HttpGet区别所在,这里是将参数用List传递
09.
10. String result="";
11.
12. HttpResponse response=new DefaultHttpClient().execute(httpPost);
13. if(response.getStatusLine().getStatusCode()==200){
14. HttpEntity entity=response.getEntity();
15. result=EntityUtils.toString(entity, HTTP.UTF_8);
16. }
17. return result;
18. }