Código
package anywheresoftware.b4a.http; import android.os.Handler; import android.util.Log; import anywheresoftware.b4a.BA; import anywheresoftware.b4a.BA.CheckForReinitialize; import anywheresoftware.b4a.BA.Hide; import anywheresoftware.b4a.BA.ShortName; import anywheresoftware.b4a.BA.Version; import anywheresoftware.b4a.objects.collections.Map; import anywheresoftware.b4a.objects.streams.File.InputStreamWrapper; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.HashMap; import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.StatusLine; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.http.conn.params.ConnManagerParams; import org.apache.http.conn.params.ConnPerRoute; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.entity.AbstractHttpEntity; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.InputStreamEntity; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.auth.DigestScheme; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; @BA.ShortName("HttpClient") @BA.Version(1.36F) public class HttpClientWrapper implements BA.CheckForReinitialize { private static final int maxConnectionToRoute = 5; @BA.Hide public DefaultHttpClient client; private String eventName; public static void LIBRARY_DOC() {} private HttpResponse executeWithTimeout(final Runnable paramRunnable, HttpUriRequest paramHttpUriRequest, BA paramBA, final int paramInt) throws ClientProtocolException, IOException { try { HttpResponse localHttpResponse = this.client.execute(paramHttpUriRequest); return localHttpResponse; } catch (ConnectionPoolTimeoutException localConnectionPoolTimeoutException) { BA.handler.postDelayed(new Runnable() { public void run() { BA.submitRunnable(paramRunnable, HttpClientWrapper.this, paramInt); } }, 2000L); } return null; } private void initializeShared(String paramString, org.apache.http.conn.ssl.SSLSocketFactory paramSSLSocketFactory) { this.eventName = paramString.toLowerCase(BA.cul); SchemeRegistry localSchemeRegistry = new SchemeRegistry(); localSchemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); localSchemeRegistry.register(new Scheme("https", paramSSLSocketFactory, 443)); BasicHttpParams localBasicHttpParams = new BasicHttpParams(); ConnManagerParams.setMaxConnectionsPerRoute(localBasicHttpParams, new ConnPerRoute() { public int getMaxForRoute(HttpRoute paramAnonymousHttpRoute) { return 5; } }); ConnManagerParams.setTimeout(localBasicHttpParams, 100L); this.client = new DefaultHttpClient(new ThreadSafeClientConnManager(localBasicHttpParams, localSchemeRegistry), localBasicHttpParams); } public boolean Execute(BA paramBA, HttpUriRequestWrapper paramHttpUriRequestWrapper, int paramInt) throws ClientProtocolException, IOException { return ExecuteCredentials(paramBA, paramHttpUriRequestWrapper, paramInt, null, null); } public boolean ExecuteCredentials(BA paramBA, HttpUriRequestWrapper paramHttpUriRequestWrapper, int paramInt, String paramString1, String paramString2) throws ClientProtocolException, IOException { if (BA.isTaskRunning(this, paramInt)) { return false; } BA.submitRunnable(new ExecuteHelper(paramBA, paramHttpUriRequestWrapper, paramInt, paramString1, paramString2), this, paramInt); return true; } public void Initialize(String paramString) throws ClientProtocolException, IOException { initializeShared(paramString, org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory()); } public void InitializeAcceptAll(String paramString) throws KeyManagementException, NoSuchAlgorithmException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { TrustManager[] arrayOfTrustManager = new TrustManager[1]; arrayOfTrustManager[0] = new NaiveTrustManager(null); SSLContext localSSLContext = SSLContext.getInstance("TLS"); localSSLContext.init(new KeyManager[0], arrayOfTrustManager, new SecureRandom()); Constructor localConstructor = org.apache.http.conn.ssl.SSLSocketFactory.class.getConstructor(new Class[] { javax.net.ssl.SSLSocketFactory.class }); Object[] arrayOfObject = new Object[1]; arrayOfObject[0] = localSSLContext.getSocketFactory(); org.apache.http.conn.ssl.SSLSocketFactory localSSLSocketFactory = (org.apache.http.conn.ssl.SSLSocketFactory)localConstructor.newInstance(arrayOfObject); localSSLSocketFactory.setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); initializeShared(paramString, localSSLSocketFactory); } public boolean IsInitialized() { return this.client != null; } public void SetHttpParameter(String paramString, Object paramObject) { this.client.getParams().setParameter(paramString, paramObject); } public void SetProxy(String paramString1, int paramInt, String paramString2) { HttpHost localHttpHost = new HttpHost(paramString1, paramInt, paramString2); this.client.getParams().setParameter("http.route.default-proxy", localHttpHost); } public void SetProxy2(String paramString1, int paramInt, String paramString2, String paramString3, String paramString4) { HttpHost localHttpHost = new HttpHost(paramString1, paramInt, paramString2); this.client.getCredentialsProvider().setCredentials(new AuthScope(paramString1, paramInt), new UsernamePasswordCredentials(paramString3, paramString4)); this.client.getParams().setParameter("http.route.default-proxy", localHttpHost); } class ExecuteHelper implements Runnable { private HttpClientWrapper.HttpUriRequestWrapper HttpRequest; private String Password; private int TaskId; private String UserName; private BA ba; public ExecuteHelper(BA paramBA, HttpClientWrapper.HttpUriRequestWrapper paramHttpUriRequestWrapper, int paramInt, String paramString1, String paramString2) { this.ba = paramBA; this.HttpRequest = paramHttpUriRequestWrapper; this.TaskId = paramInt; this.UserName = paramString1; this.Password = paramString2; } public void run() { HttpResponse localHttpResponse = null; for (;;) { int i2; try { if (((this.HttpRequest.req instanceof HttpEntityEnclosingRequestBase)) && (this.UserName != null) && (this.UserName.length() > 0)) { HttpEntityEnclosingRequestBase localHttpEntityEnclosingRequestBase = (HttpEntityEnclosingRequestBase)this.HttpRequest.req; HttpEntity localHttpEntity = localHttpEntityEnclosingRequestBase.getEntity(); localHttpResponse = null; if (localHttpEntity != null) { boolean bool = localHttpEntityEnclosingRequestBase.getEntity().isRepeatable(); localHttpResponse = null; if (!bool) { UsernamePasswordCredentials localUsernamePasswordCredentials2 = new UsernamePasswordCredentials(this.UserName, this.Password); Header localHeader3 = new BasicScheme().authenticate(localUsernamePasswordCredentials2, this.HttpRequest.req); this.HttpRequest.req.addHeader(localHeader3); } } } localHttpResponse = HttpClientWrapper.this.executeWithTimeout(this, this.HttpRequest.req, this.ba, this.TaskId); if (localHttpResponse == null) { return; } if ((localHttpResponse.getStatusLine().getStatusCode() == 401) && (this.UserName != null) && (this.UserName.length() > 0)) { m = 0; n = 0; localObject = null; arrayOfHeader = localHttpResponse.getHeaders("WWW-Authenticate"); int i1 = arrayOfHeader.length; i2 = 0; if (i2 < i1) { continue; } localUsernamePasswordCredentials1 = new UsernamePasswordCredentials(this.UserName, this.Password); if (localHttpResponse.getEntity() != null) { localHttpResponse.getEntity().consumeContent(); } if (n == 0) { continue; } DigestScheme localDigestScheme = new DigestScheme(); localDigestScheme.processChallenge((Header)localObject); this.HttpRequest.req.addHeader(localDigestScheme.authenticate(localUsernamePasswordCredentials1, this.HttpRequest.req)); localHttpResponse = HttpClientWrapper.this.executeWithTimeout(this, this.HttpRequest.req, this.ba, this.TaskId); if (localHttpResponse == null) { break label950; } } } catch (Exception localException1) { int m; int n; Object localObject; Header[] arrayOfHeader; UsernamePasswordCredentials localUsernamePasswordCredentials1; if (localHttpResponse == null) { continue; } String str1 = localHttpResponse.getStatusLine().getReasonPhrase(); int i = localHttpResponse.getStatusLine().getStatusCode(); Method localMethod = (Method)this.ba.htSubs.get(HttpClientWrapper.this.eventName + "_responseerror"); int j = 1; if (localMethod == null) { continue; } if ((localMethod.getParameterTypes().length != 4) && (!BA.shellMode)) { continue; } HttpClientWrapper.HttpResponeWrapper localHttpResponeWrapper1 = null; if (localHttpResponse == null) { continue; } localHttpResponeWrapper1 = new HttpClientWrapper.HttpResponeWrapper(); HttpClientWrapper localHttpClientWrapper1 = HttpClientWrapper.this; HttpClientWrapper.HttpResponeWrapper.access$0(localHttpResponeWrapper1, localHttpClientWrapper1); HttpClientWrapper.HttpResponeWrapper.access$1(localHttpResponeWrapper1, localHttpResponse); try { ByteArrayEntity localByteArrayEntity = new ByteArrayEntity(EntityUtils.toByteArray(localHttpResponse.getEntity())); localHttpResponse.setEntity(localByteArrayEntity); arrayOfObject1 = new Object[4]; arrayOfObject1[0] = localHttpResponeWrapper1; arrayOfObject1[1] = str1; arrayOfObject1[2] = Integer.valueOf(i); arrayOfObject1[3] = Integer.valueOf(this.TaskId); j = 0; this.ba.raiseEventFromDifferentThread(HttpClientWrapper.this.client, HttpClientWrapper.this, this.TaskId, HttpClientWrapper.this.eventName + "_responseerror", false, arrayOfObject1); if ((j == 0) || (localHttpResponse == null) || (localHttpResponse.getEntity() == null)) { break label950; } try { localHttpResponse.getEntity().consumeContent(); return; } catch (IOException localIOException) { Log.w("B4A", localIOException); return; } Header localHeader2 = arrayOfHeader[i2]; String str3 = localHeader2.getValue().toLowerCase(BA.cul); if (str3.contains("basic")) { m = 1; break label951; } if (!str3.contains("digest")) { break label951; } n = 1; localObject = localHeader2; break label951; if (m == 0) { continue; } Header localHeader1 = new BasicScheme().authenticate(localUsernamePasswordCredentials1, this.HttpRequest.req); this.HttpRequest.req.addHeader(localHeader1); localHttpResponse = HttpClientWrapper.this.executeWithTimeout(this, this.HttpRequest.req, this.ba, this.TaskId); if (localHttpResponse != null) { continue; } return; HttpClientWrapper.HttpResponeWrapper localHttpResponeWrapper2 = new HttpClientWrapper.HttpResponeWrapper(); HttpClientWrapper.HttpResponeWrapper.access$0(localHttpResponeWrapper2, HttpClientWrapper.this); HttpClientWrapper.HttpResponeWrapper.access$1(localHttpResponeWrapper2, localHttpResponse); BA localBA = this.ba; DefaultHttpClient localDefaultHttpClient = HttpClientWrapper.this.client; HttpClientWrapper localHttpClientWrapper2 = HttpClientWrapper.this; int k = this.TaskId; String str2 = HttpClientWrapper.this.eventName + "_responsesuccess"; Object[] arrayOfObject2 = new Object[2]; arrayOfObject2[0] = localHttpResponeWrapper2; arrayOfObject2[1] = Integer.valueOf(this.TaskId); localBA.raiseEventFromDifferentThread(localDefaultHttpClient, localHttpClientWrapper2, k, str2, true, arrayOfObject2); return; localException1.printStackTrace(); str1 = localException1.toString(); i = -1; } catch (Exception localException2) { localException2.printStackTrace(); continue; } Object[] arrayOfObject1 = new Object[3]; arrayOfObject1[0] = str1; arrayOfObject1[1] = Integer.valueOf(i); arrayOfObject1[2] = Integer.valueOf(this.TaskId); continue; } if (localHttpResponse.getStatusLine().getStatusCode() / 100 != 2) { throw new Exception(); } label950: return; label951: i2++; } } } @BA.ShortName("HttpResponse") public static class HttpResponeWrapper { private HttpClientWrapper parent; private HttpResponse response; static Map convertHeaders(Header[] paramArrayOfHeader) { Map localMap = new Map(); localMap.Initialize(); int i = paramArrayOfHeader.length; for (int j = 0;; j++) { if (j >= i) { return localMap; } Header localHeader = paramArrayOfHeader[j]; java.util.List localList = (java.util.List)localMap.Get(localHeader.getName()); if (localList == null) { anywheresoftware.b4a.objects.collections.List localList1 = new anywheresoftware.b4a.objects.collections.List(); localList1.Initialize(); localList = (java.util.List)localList1.getObject(); localMap.Put(localHeader.getName(), localList); } localList.add(localHeader.getValue()); } } private void innerInitialize(HttpClientWrapper paramHttpClientWrapper) { this.parent = paramHttpClientWrapper; } public boolean GetAsynchronously(final BA paramBA, final String paramString, final OutputStream paramOutputStream, final boolean paramBoolean, final int paramInt) throws IOException { if (BA.isTaskRunning(this.parent, paramInt)) { Release(); return false; }