get和post请求方式
来源:互联网 发布:淘宝直通车创意主图 编辑:程序博客网 时间:2024/05/20 10:55
前言:在Android开发的过程中,必须会接触到数据交互(访问数据,写入数据等你等),既然接触到数据的交互,那么自然而然就是使用通讯间的协议来进行请求,最常见的协议就是Http协议,Http协议包括两个具体的请求方式-Get以及Post。
- Http请求方式Get与Post的简介
先来了解Http协议:Http(HyperText Transfer Protocol超文本传输协议)是一个设计来使客户端和服务器顺利进行通讯的协议。
HTTP在客户端和服务器之间以request-response protocol(请求-回复协议)工作。
简单来说呢,Get与Post就是基于http协议的网络数据交互方式。
- Http请求方式Get与Post的简介
- Get与Post的主要区别
在Android开发的过程中,该如何选择Http的Get还是Post来进行通讯呢?那就详细探索他们之间的差异。
1.get通常是从服务器上获取数据,post通常是向服务器传送数据。
2.get是把参数数据队列加到表单的 ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到,实际上就是URL拼接方式。post是通过HTTPpost机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。
3.对于get方式,服务器端用 Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
4.get 传送的数据量较小,不能大于1KB[IE,Oher:4]。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
5.get安全性非常低,post安全性较高。
- Get与Post的主要区别
- Android如何使用Get与Post协议
不多说,上代码展示(演示用户登录访问服务器)
/** * post的方式请求 *@param User用户名 *@param Password 密码 */
public static String setPostUrl(final String httpUrl, final String post) { String result = null; try { byte[] bs = post.getBytes(); URL url = new URL(httpUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true);//是否可写 conn.setDoOutput(true);//是否可读 conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); OutputStream os = conn.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.write(bs); dos.close(); dos.flush(); os.close(); InputStream is = conn.getInputStream(); //字节流转换成字符流 InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); //可以拼接字符串 StringBuffer sb = new StringBuffer(); String str; char c[] = new char[8 * 1024]; //边度编写,实现 缓冲字符流的 readLine 一行 while ((str = br.readLine()) != null) { sb.append(str); } result = sb.toString(); // //获得结果码 优化的方式 // int responseCode = conn.getResponseCode(); // if(responseCode ==200){ // //请求成功 // }else { // //请求失败 // } } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
/** *get的方式请求 *@param username 用户名 *@param password 密码 *@return 返回null 登录异常 */
public void startLogin() { new Thread(new Runnable() { @Override public void run() { try { String User = mLogin_User.getText().toString(); String Password = mLogin_Password.getText().toString(); URL url = new URL(HttpUtils.urlLogin); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true);//是否可写 conn.setDoOutput(true);//是否可读 conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); InputStream is = conn.getInputStream(); //字节流转换成字符流 InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); //可以拼接字符串 StringBuffer sb = new StringBuffer(); String str; char c[] = new char[8 * 1024]; //边度编写,实现 缓冲字符流的 readLine 一行 while ((str = br.readLine()) != null) { sb.append(str); } String result = sb.toString(); Log.e(Tag, result); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); }}
阅读全文
0 0
- 请求方式GET和POST
- get和post请求方式
- get和post请求方式
- get和post请求方式的区别
- post和get请求方式以及区别
- post和get请求方式的区别
- get 和post 方式请求数据
- get和post请求方式的区别
- post和get请求方式以及区别
- 请求方式Get和Post的区别
- HTTP 请求方式: GET和POST
- java网络请求GET和POST方式
- PHP异步请求 POST和GET方式
- get方式和post请求的区别?
- Post和Get请求方式的区别
- Http请求get和post方式
- GET和POST请求方式的区别
- http的post和get请求方式
- C# serialPort 串口接收中this.Invoke的使用
- 使用Jmeter进行http接口测试
- android:android6.0运行权限管理
- 程序设计模式(十二) C++ 代理(Proxy)模式
- 新闻客户端
- get和post请求方式
- linux命令英文缩写的含义(方便记忆)
- Oracle数据库性能调优第一节
- 简单的瀑布流的实现
- STL中查找方法用法总结-adjacent_find, binary_search, includes, lower_bound, upper_bound, equal_range
- BZOJ2038: [2009国家集训队]小Z的袜子(hose)
- Ambari数据库分析-配置相关
- codeforces 793B
- 用一台笔记本跑ROVIO