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 scheme 2 httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory()); 3 httpClient.getCredentialsProvider().setCredentials( 4 // Limit the credentials only to the specified domain and port 5 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 6 // Specify credentials, most of the time only user/pass is needed 7 new NTCredentials(userName, password, "", "") 8 );
直接替换 //基本验证 即可使用
使用jcifs-1.3.17.jar(可到https://github.com/masconsult/android-ntlm 进行下载)
1 public class NTLMSchemeFactory implements AuthSchemeFactory 2 { 3 @Override 4 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 @Override 10 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 @Override 18 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 = type2Flags 31 & (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 }
时间: 2024-11-05 16:27:14