【转载】Android基于HTTP header的用户Authentication
来源:互联网 发布:晋业进销存软件8.25 编辑:程序博客网 时间:2024/06/11 16:53
转自我的github博客,In Pursuit of Absolute Simplicity
博文地址,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: 308POST /test.php HTTP/1.1Accept: application/jsonUser-Agent: Apache-HttpClient/4.1 (java 1.5)Host: myhost.comAuthorization: Basic YnxpcYRlc3RwMTulHGhlSGs=Content-Length: 21Content-Type: application/x-www-form-urlencoded; charset=UTF-8Content-Encoding: UTF-8Connection: Keep-AliveHTTP/1.1 200 OKContent-type: text/htmlTransfer-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 informationhttpReq.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(username, password),"UTF-8", false));DefaultHttpClient httpClient = new DefaultHttpClient();HttpResponse httpResponse = httpClient.execute(httpReq);// handler respondsStringBuilder 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 cookieCookieStore cookieStore = ((AbstractHttpClient) httpClient).getCookieStore();} catch (Exception e) {Log.e("", e.toString());}return res;}}
- 【转载】Android基于HTTP header的用户Authentication
- HTTP Basic Authentication认证方式和AFNetworking的header的设置
- BASIC HTTP AUTHENTICATION ON ANDROID
- BASIC HTTP AUTHENTICATION ON ANDROID
- HTTP的Header信息
- HTTP的Header
- http的header
- http 的header
- 非标准的http header
- 非标准的http header
- http-header的理解
- 我的Android进阶之旅------>HTTP Header 详解
- HTTP Authentication
- The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the serv
- nginx用户认证配置( Basic HTTP authentication)
- nginx用户认证配置( Basic HTTP authentication)
- http协议header的内容
- HTTP的Header信息详解
- poj1833 排列
- 一步一步学数据结构之1--n(二叉树遍历--四种方法--递归式)
- mmc换硬币问题
- vi / vim 删除以及其它命令
- 易用的C++ RPC服务框架 - pioneer - 5 - 技术实现:函数的序列化
- 【转载】Android基于HTTP header的用户Authentication
- leetcode length of last word
- 书写CSS的5个小技巧
- 如何证明非方阵的矩阵是否可逆
- Google C++编程命名约定
- 3dmax中cs骨骼教程
- [3dmax教程] 人物+骨骼+蒙皮+动画教程
- .在二元树中找出和为某一值的所有路径
- SQL SERVER 创建GHUID命令:select newid();