public static String getUrlFollowRedirects(String url, int maxRetries) throws IOException {
    String result = null;
    int retries = 0;
    DefaultHttpClient httpclient = new DefaultHttpClient(defaultHttpParams);
    // Agregado para LinkTel
    httpclient.setRedirectHandler(new DefaultRedirectHandler());
    httpclient.setCookieStore(new BasicCookieStore());
    HttpGet httpget = new HttpGet(url);
    while (retries <= maxRetries && result == null) {
      try {
        retries++;
        HttpEntity entity = httpclient.execute(httpget).getEntity();

        if (entity != null) {
          result = EntityUtils.toString(entity).trim();
        }
      } catch (SocketException se) {
        if (retries > maxRetries) {
          throw se;
        } else {
          Log.v(TAG, "SocketException, retrying " + retries);
        }
      }
    }

    return result;
  }
  /**
   * new {@link DefaultHttpClient}, and set strategy.
   *
   * @return DefaultHttpClient
   */
  private DefaultHttpClient createApacheHttpClient(BasicHttpParams httpParams) {
    DefaultHttpClient httpClient =
        new DefaultHttpClient(createClientConnManager(httpParams), httpParams);
    // disable apache default redirect handler
    httpClient.setRedirectHandler(
        new RedirectHandler() {

          @Override
          public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
            return false;
          }

          @Override
          public URI getLocationURI(HttpResponse response, HttpContext context)
              throws ProtocolException {
            return null;
          }
        });
    // disable apache default retry handler
    httpClient.setHttpRequestRetryHandler(
        new HttpRequestRetryHandler() {
          @Override
          public boolean retryRequest(
              IOException exception, int executionCount, HttpContext context) {
            return false;
          }
        });
    // enable gzip supporting in request
    httpClient.addRequestInterceptor(
        new HttpRequestInterceptor() {
          @Override
          public void process(org.apache.http.HttpRequest request, HttpContext context) {
            if (!request.containsHeader(Consts.HEADER_ACCEPT_ENCODING)) {
              request.addHeader(Consts.HEADER_ACCEPT_ENCODING, Consts.ENCODING_GZIP);
            }
          }
        });
    // enable gzip supporting in response
    httpClient.addResponseInterceptor(
        new HttpResponseInterceptor() {
          @Override
          public void process(HttpResponse response, HttpContext context) {
            final HttpEntity entity = response.getEntity();
            if (entity == null) {
              return;
            }
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
              for (HeaderElement element : encoding.getElements()) {
                if (element.getName().equalsIgnoreCase(Consts.ENCODING_GZIP)) {
                  response.setEntity(new GZIPEntityWrapper(entity));
                  break;
                }
              }
            }
          }
        });
    // setKeepAlive(httpClient);
    return httpClient;
  }
Example #3
0
  public static String httpGetSSL(final String exist, String database, String android_id)
      throws IOException, HttpHostConnectException, ConnectException {
    String getUrl = "https://" + exist + ":50001/exist/rest/db/calendar/" + android_id + ".xml";
    LogHelper.logV(c, "httpGetSSL: " + getUrl);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    //        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(),
    // 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    ClientConnectionManager ccm = new SingleClientConnManager(params, schemeRegistry);

    httpClient =
        new DefaultHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params);
    httpClient.setRedirectHandler(new DefaultRedirectHandler());

    HttpGet get = new HttpGet(getUrl);

    get.setHeader("Location", database + ":50082");
    get.setHeader("Connection", "close");

    HttpResponse response = httpClient.execute(get);

    HttpEntity httpEntity = response.getEntity();

    return EntityUtils.toString(httpEntity);
  }
Example #4
0
    @Override
    protected String doInBackground(NetTaskRequest... params) {

      String result = null;

      this.request = params[0];

      HttpUriRequest request = this.request.getRequest();
      DefaultHttpClient client = new DefaultHttpClient();
      client.setRedirectHandler(new DefaultRedirectHandler());

      try {

        HttpResponse response = client.execute(request);
        InputStream is = response.getEntity().getContent();

        Scanner scanner = new Scanner(is);
        result = scanner.useDelimiter("\\A").next();
        scanner.close();

        is.close();

      } catch (ClientProtocolException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }

      return result;
    }
  /**
   * Get's an SSO Uri or null if there are any errors
   *
   * @return SSO uri or null
   */
  protected Uri doGetSSO() {

    if (user == null || pass == null) {
      return null;
    }

    DefaultHttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, App.userAgent);
    // register ntlm auth scheme
    client.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
    client
        .getCredentialsProvider()
        .setCredentials(
            // Limit the credentials only to the specified domain and port
            new AuthScope("portal.myfanshawe.ca", -1),
            // Specify credentials, most of the time only user/pass is needed
            new NTCredentials(user, pass, "", ""));

    final String[] ssoUrl = {null};
    final RedirectHandler defaultHandler = client.getRedirectHandler();
    client.setRedirectHandler(
        new RedirectHandler() {
          @Override
          public boolean isRedirectRequested(HttpResponse httpResponse, HttpContext httpContext) {
            Log.i(TAG, "isRedirectRequested");
            for (Header header : httpResponse.getAllHeaders()) {
              String name = header.getName();
              String value = header.getValue();
              if ("Location".equals(name)) {
                ssoUrl[0] = value;
              }
            }
            return false;
          }

          @Override
          public URI getLocationURI(HttpResponse httpResponse, HttpContext httpContext)
              throws ProtocolException {
            return defaultHandler.getLocationURI(httpResponse, httpContext);
          }
        });

    HttpGet folSSO = new HttpGet(requestURL);

    try {
      HttpResponse response = client.execute(folSSO);
      HttpEntity entity = response.getEntity();
      entity.consumeContent();
      Log.i(TAG, "SSO OK");
    } catch (IOException e) {
      return null;
    }

    if (ssoUrl[0] == null) {
      return null;
    }
    return Uri.parse(ssoUrl[0]);
  }
 /**
  * Simple interface method, to enable or disable redirects. If you set manually RedirectHandler on
  * underlying HttpClient, effects of this method will be canceled.
  *
  * @param enableRedirects boolean
  */
 public void setEnableRedirects(final boolean enableRedirects) {
   httpClient.setRedirectHandler(
       new DefaultRedirectHandler() {
         @Override
         public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
           return enableRedirects;
         }
       });
 }
Example #7
0
  /** httpPost */
  public static String httpPost(final String url) throws IOException {

    LogHelper.logV(c, "httpPost: " + url);

    DefaultHttpClient httpClient = new DefaultHttpClient();
    ClientConnectionManager ccm = httpClient.getConnectionManager();
    HttpParams params = httpClient.getParams();

    httpClient =
        new DefaultHttpClient(
            new ThreadSafeClientConnManager(params, ccm.getSchemeRegistry()), params);
    httpClient.setRedirectHandler(new DefaultRedirectHandler());

    HttpPost post = new HttpPost(url);

    post.setHeader("Location", "localhost:50082");
    post.setHeader("Connection", "close");
    post.setHeader("Content-Type", "text/xml");

    HttpResponse response = httpClient.execute(post);

    HttpEntity httpEntity = response.getEntity();
    return EntityUtils.toString(httpEntity);
  }