標籤:
1)基礎驗證:
1 /** 2 * 從伺服器取圖片 3 * 4 * @param url 5 * @return 6 */ 7 public void getHttpBitmap(final String url) { 8 new Thread(new Runnable() { 9 public void run() {10 Bitmap bitmap = null;11 try {12 HttpGet httpPost = new HttpGet(url);13 DefaultHttpClient httpClient = new DefaultHttpClient();14 // 基本驗證15 BasicCredentialsProvider bcp = new BasicCredentialsProvider();16 String userName = "username";17 String password = "password";18 AuthScope authScope = new AuthScope(AuthScope.ANY);19 bcp.setCredentials(authScope ,20 new UsernamePasswordCredentials(userName, password));21 22 httpClient.setCredentialsProvider(bcp);23
//基本驗證結束
HttpResponse httpResponse = httpClient.execute(httpPost);24 android.util.Log.i("", "=====>" + httpResponse.getStatusLine().getStatusCode());25 android.util.Log.i("", "=====>" + httpResponse.getEntity().getContentLength());26 InputStream is = httpResponse.getEntity().getContent();27 bitmap = BitmapFactory.decodeStream(is);28 is.close();29 } catch (Exception e) {30 e.printStackTrace();31 }32 33 getResult(bitmap);34 }35 }).start();36 }
1 byte[] encodedPassword = (userName + ":" + password).getBytes();2 httpPost.addHeader("Authorization","Base " + Base64.encodetoString(encodedPassword, Base64.No_Wrap);
2)ntlm 身分識別驗證:
1 // register ntlm auth scheme2 httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());3 httpClient.getCredentialsProvider().setCredentials(4 // Limit the credentials only to the specified domain and port5 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),6 // Specify credentials, most of the time only user/pass is needed7 new NTCredentials(userName, password, "", "")8 );
直接替換 //基本驗證 即可使用
使用jcifs-1.3.17.jar(可到https://github.com/masconsult/android-ntlm 進行下載)
1 public class NTLMSchemeFactory implements AuthSchemeFactory2 {3 @Override4 public AuthScheme newInstance(HttpParams params)5 {6 return new NTLMScheme(new JCIFSEngine());7 }8 }
1 public class JCIFSEngine implements NTLMEngine { 2 3 private static final int TYPE_1_FLAGS = NtlmFlags.NTLMSSP_NEGOTIATE_56 4 | NtlmFlags.NTLMSSP_NEGOTIATE_128 5 | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 6 | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN 7 | NtlmFlags.NTLMSSP_REQUEST_TARGET; 8 9 @Override10 public String generateType1Msg(String domain, String workstation)11 throws NTLMEngineException {12 final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS,13 domain, workstation);14 return Base64.encode(type1Message.toByteArray());15 }16 17 @Override18 public String generateType3Msg(String username, String password,19 String domain, String workstation, String challenge)20 throws NTLMEngineException {21 Type2Message type2Message;22 23 try {24 type2Message = new Type2Message(Base64.decode(challenge));25 } catch (final IOException exception) {26 throw new NTLMEngineException("Error in type2 message", exception);27 }28 29 final int type2Flags = type2Message.getFlags();30 final int type3Flags = type2Flags31 & (0xffffffff ^ (NtlmFlags.NTLMSSP_TARGET_TYPE_DOMAIN | NtlmFlags.NTLMSSP_TARGET_TYPE_SERVER));32 final Type3Message type3Message = new Type3Message(type2Message,33 password, domain, username, workstation, type3Flags);34 return Base64.encode(type3Message.toByteArray());35 }36 }
Android 與 IIS伺服器身分識別驗證