@Override protected final void receiveMsgs() { /*SchemeRegistry supportedSchemes = new SchemeRegistry(); SocketFactory sf = PlainSocketFactory.getSocketFactory(); supportedSchemes.register(new Scheme("http", sf, 80)); ClientConnectionManager ccm = new SingleClientConnManager(httpParams, supportedSchemes); DefaultHttpClient client = new DefaultHttpClient(httpParams); if (getUsername() != null) { client.getCredentialsProvider().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(getUsername(), getPassword())); if (isPreemtiveAuth()) { client.addRequestInterceptor(new PreemptiveAuth(), 0); } if (loginAction != null) loginAction.doAction(client, this); }*/ if (httpClient == null) httpClient = new DefaultHttpClient(); if (getUsername() != null) { ((DefaultHttpClient) httpClient) .getCredentialsProvider() .setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(getUsername(), getPassword())); } receiveMsgs(httpClient); }
/** * Sets basic authentication on web request using plain credentials * * @param username The plain text username * @param password The plain text password */ public void setBasicAuthentication(String username, String password) { ((DefaultHttpClient) client) .getCredentialsProvider() .setCredentials( new AuthScope( postMethod.getURI().getHost(), postMethod.getURI().getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password)); }
/** * Convenience constructor. Creates new instance based on server String address * * @param XMLRPC server address * @param HTTP Server - Basic Authentication - Username * @param HTTP Server - Basic Authentication - Password */ public XMLRPCClient(URI uri, String username, String password) { this(uri); ((DefaultHttpClient) client) .getCredentialsProvider() .setCredentials( new AuthScope(uri.getHost(), uri.getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password)); }
/** * Sets basic authentication on web request using plain credentials * * @param username The plain text username * @param password The plain text password * @param doPreemptiveAuth Select here whether to authenticate without it being requested first by * the server. */ public void setBasicAuthentication(String username, String password, boolean doPreemptiveAuth) { // This code required to trigger the patch created by erickok in issue #6 if (doPreemptiveAuth = true) { this.httpPreAuth = doPreemptiveAuth; this.username = username; this.password = password; } else { ((DefaultHttpClient) client) .getCredentialsProvider() .setCredentials( new AuthScope( postMethod.getURI().getHost(), postMethod.getURI().getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password)); } }
protected void initAuthentication() { if (registration.getAuthenticationMechanism() != null) { if (registration.getAuthenticationMechanism().getType() instanceof BasicAuth) { BasicAuth basic = (BasicAuth) registration.getAuthenticationMechanism().getType(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(basic.getUsername(), basic.getPassword()); AuthScope authScope = new AuthScope(AuthScope.ANY); ((DefaultHttpClient) client).getCredentialsProvider().setCredentials(authScope, creds); localContext = new BasicHttpContext(); // Generate BASIC scheme object and stick it to the local execution context BasicScheme basicAuth = new BasicScheme(); localContext.setAttribute("preemptive-auth", basicAuth); // Add as the first request interceptor ((DefaultHttpClient) client).addRequestInterceptor(new PreemptiveAuth(), 0); executor.setHttpContext(localContext); } } }
/** * Setup request. * * @param path the path * @param method the method * @param params the vars * @return the http uri request */ private HttpUriRequest setupRequest( String path, final String method, final List<NameValuePair> params) { String normalizedPath = path.toLowerCase(); StringBuilder sb = new StringBuilder(); // If we've given a fully qualified uri then skip building the endpoint if (normalizedPath.startsWith("http://") || normalizedPath.startsWith("https://")) { sb.append(path); } else { sb.append(getEndpoint()); if (!normalizedPath.startsWith("/")) { sb.append("/"); } sb.append(path); } path = sb.toString(); HttpUriRequest request = buildMethod(method, path, params); request.addHeader(new BasicHeader("X-Twilio-Client", "java-" + VERSION)); request.addHeader(new BasicHeader("User-Agent", "twilio-java/" + VERSION)); request.addHeader(new BasicHeader("Accept", "application/json")); request.addHeader(new BasicHeader("Accept-Charset", "utf-8")); if (httpclient instanceof DefaultHttpClient) { // as DefaultHttpClient class has final method, I need httpClient to be // a plain interface to be able to mock it ((DefaultHttpClient) httpclient) .getCredentialsProvider() .setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password)); } return request; }
/** * 获得HttpClient * * @param context * @return HttpClient */ public static synchronized DefaultHttpClient getHttpClient(Context context) { mcontext = context; // registNetChangeBroadcast(); if (null == customerHttpClient || networkchanged) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, CHARSET); HttpProtocolParams.setUseExpectContinue(params, true); // HttpProtocolParams.setUserAgent(params, USERAGENT); // ConnManagerParams.setTimeout(params, 15000); // HttpConnectionParams.setConnectionTimeout(params, 15000); HttpConnectionParams.setSoTimeout(params, 15000); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); customerHttpClient = new DefaultHttpClient(conMgr, params); // new DefaultHttpClient(); // customerHttpClient.getParams().setParameter("netmac", Utils.getMacAdress(mcontext)); customerHttpClient.getParams().setParameter("version", Utils.getVersion(mcontext)); // 添加证书 enableSSL(customerHttpClient); // String host=Proxy.getDefaultHost();//此处Proxy源自android.net // int port = Proxy.getDefaultPort();//同上 // Log.i(TAG, "-----------host="+host+"------port="+port); //Proxy:10.0.0.200 port:80 ConnectivityManager conManager = (ConnectivityManager) mcontext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); String currentAPN = info.getExtraInfo(); // Log.i(TAG, "----- ConnectivityManager ------currentAPN="+currentAPN); if (!isWifiConnected(mcontext)) { // 设置代理 if ("ctwap".equalsIgnoreCase(currentAPN)) { // 电信 HttpHost httpHost = new HttpHost("10.0.0.200", 80); customerHttpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, httpHost); Credentials defaultcreds = new UsernamePasswordCredentials("*****@*****.**", "vnet.mobi"); ((DefaultHttpClient) customerHttpClient) .getCredentialsProvider() .setCredentials(new AuthScope("10.0.0.200", 80, AuthScope.ANY_REALM), defaultcreds); } else if ("3gwap".equalsIgnoreCase(currentAPN)) { // 联通 HttpHost httpHost = new HttpHost("10.0.0.172", 80); customerHttpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, httpHost); } else if ("cmwap".equalsIgnoreCase(currentAPN)) { // 移动 HttpHost httpHost = new HttpHost("10.0.0.172", 80); customerHttpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, httpHost); } } else { // Log.i(TAG, "----------isWifiConnected-------- "); } } networkchanged = false; return customerHttpClient; }