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



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());
		//将加密的信息转换为string
		String 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 block
		e.printStackTrace();
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}



		return null;
	}

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

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

你可能感兴趣的:(Authorization,登陆,httppost)