【转载】Android基于HTTP header的用户Authentication
2013-08-13 05:42
232 查看
转自我的github博客,In Pursuit of Absolute Simplicity
博文地址,http://jesusjzp.github.io/blog/2013/08/12/Android-Develop-Record/
目前项目因为临时没有建立用户数据,所以选择使用http header加载用户信息,通常http头部信息如下:
Android代码如下:
博文地址,http://jesusjzp.github.io/blog/2013/08/12/Android-Develop-Record/
目前项目因为临时没有建立用户数据,所以选择使用http header加载用户信息,通常http头部信息如下:
2011-02-23 15:37:36: (request.c.304) fd: 8 request-len: 308 POST /test.php HTTP/1.1 Accept: application/json User-Agent: Apache-HttpClient/4.1 (java 1.5) Host: myhost.com Authorization: Basic YnxpcYRlc3RwMTulHGhlSGs= Content-Length: 21 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Content-Encoding: UTF-8 Connection: Keep-Alive HTTP/1.1 200 OK Content-type: text/html Transfer-Encoding: chunked
Android代码如下:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.apache.http.HttpResponse; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CookieStore; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.AbstractHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import android.util.Log; public class HttpGetMethod { public String getFoldersStats(String username, String password, String action) { String res = ""; try { String url = URL ADDRESS+action; HttpGet httpReq = new HttpGet(url); // add header information httpReq.addHeader(BasicScheme.authenticate( new UsernamePasswordCredentials(username, password), "UTF-8", false)); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpReq); // handler responds StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader( httpResponse.getEntity().getContent())); for (String s = reader.readLine(); s != null; s = reader.readLine()) { builder.append(s); } res = builder.toString(); // Save to cookie CookieStore cookieStore = ((AbstractHttpClient) httpClient).getCookieStore(); } catch (Exception e) { Log.e("", e.toString()); } return res; } }
相关文章推荐
- 【转载】 javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册 - 孤傲苍狼 - 博 http://www.cnblogs.com/xdp-gacl/
- EasyPlayer Android基于ffmpeg实现播放(RTSP/RTMP/HTTP/HLS)同步录像功能
- 基于netfilter通过iptables过滤http请求使用netlink通知用户空间构造发送http响应(一)
- 基于HTTP协议的C/S架构中服务器如何获取用户身份
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- 【转载】基于android的远程视频监控系统(已开放源码)_只有链接
- 【转载】Android消息推送(二)--基于MQTT协议实现的推送功能
- 【转载】Android HttpClient基本使用方法
- Android LayoutInflater详解 (转载:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html)
- Android开发之网络请求通信专题(一):基于HttpURLConnection的请求通信
- 【安卓开发】【转载】提升基于英特尔®架构的 Android* 模拟器的速度
- js判断用户来路url(使用http header referer)
- eclipse中安装ADT插件的最简单的方法转载自http://www.cnblogs.com/go-onxp/archive/2012/05/22/android.html
- android网络编程(httpurlconnect接口,httpclient接口,与socket接口)--转载整理
- 小议基于Android平台的流媒体播放器的设计 转载
- Android实战简易教程<二十四>(基于Baas的用户表查询功能实现!)
- webapi swagger自定义 HTTP Header验证用户
- 基于NDK的Android防破解& Android防破解 【转载】
- 基于ArcGIS Engine + C#实现用户自定义动态电力符号(转载 华立电网北京研发中心 阿文 )