用HttpPost登陆验证时,用户名和密码放在请求头部header中的处理方法,形式为Authorization: username password。

来源:互联网 发布:李米的猜想台词知乎 编辑:程序博客网 时间:2024/06/07 06:12


post.setHeader("Authorization", "your token");

这里主要是要搞清楚your token是什么,把认证信息传递正确,这个认证信息是通过用户名和密码来确定的

public static User getUserData(String url,String userName,String userPass){ //1.创建 HttpClient 的实例  try {BasicHttpParams httpParams = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);DefaultHttpClient client = new DefaultHttpClient(httpParams);HttpPost post = new HttpPost(url);post.setHeader("Content-Type", "application/json");//使用base64进行加密byte[] tokenByte = Base64.encodeBase64((userName+":"+userPass).getBytes());//将加密的信息转换为stringString tokenStr = DataTypeChange.bytesSub2String(tokenByte, 0, tokenByte.length);//Basic YFUDIBGDJHFK78HFJDHF==    token的格式String token = "Basic "+tokenStr;//把认证信息发到header中post.setHeader("Authorization", "Basic "+token);HttpResponse response = client.execute(post);int statusCode = response.getStatusLine().getStatusCode();String retSrc = EntityUtils.toString(response.getEntity(), "utf-8");Log.i("Tag", "url=="+url);Log.i("Tag", "===statuscode==="+statusCode+"===retsrc==="+retSrc);} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}

有些是通过post json数据来传递用户名和密码进行登陆的

还可以通过HttpGet来验证登陆,这种情况下url中带有用户名和密码的信息

0 0
原创粉丝点击