hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Arturo Esquivel Sanchez" <ar...@menetas.net>
Subject HttpClient 2.0 problems
Date Thu, 10 Jun 2004 10:19:53 GMT



Hi,

The problem that im having is that sometimes i get the following error
when trying to connect to url, and then i change something in my code and
suddenly works fine, then i change something again and i get again the
same error (¿?), if a loop in my code in order to get a response other
than -1 it doesn´t work and i get a lots of the error.
This is the error:

[INFO] HttpMethodBase - -Recoverable exception caught when processing
request [WARN] HttpMethodBase - -Recoverable exception caught but
MethodRetryHandler.ret ryMethod() returned false, rethrowing exceptionA
recoverableexception occurred,  retrying.
org.apache.commons.httpclient.Http RecoverableException: Error in parsing
the status  line from the response: unabl e to find line starting with
"HTTP"
This is the code that im using:

      boolean retry=false;

      do{

        statusCode = Procesa_Get_Url(url, client, config, state,
        urlToConnect2, respHTML1);
        if (statusCode != -1 && urlToConnect2.toString().trim() != "" &&
        urlToConnect2.toString().trim().length() != 0){
        retry=true;        }
      }while (retry==false);


 private int Procesa_Get_Url(String urlProc, HttpClient clientProc,
 HostConfiguration configProc, HttpState stateProc, StringBuffer urlTC,
 StringBuffer respHTML) throws Exception {    int statusCode=-1;
    String redirectLocation=null;

    clientProc.setTimeout(60*30*1000);
    clientProc.setHttpConnectionFactoryTimeout(60*30*1000);
    clientProc.setConnectionTimeout(60*30*1000);

    //try{

      //URI uri = new URI(urlProc.toCharArray());
      URI uri = new URI(urlProc.toCharArray());

      HttpMethod method = new GetMethod(uri.toString());

      String schema = uri.getScheme();

      if ((schema == null) || (schema.equals(""))){
            schema = "http";
      }

      Protocol protocol = Protocol.getProtocol(schema);

      String host = uri.getHost();
      int port = uri.getPort();

      configProc.setHost(host,port,protocol);
      configProc.setProxy(System.getProperty("http.proxyHost"),Integer.parseInt(System.getProperty("http.proxyPort","80")));
          stateProc.setProxyCredentials(null, null,
        new UsernamePasswordCredentials(
          System.getProperty("http.proxyUserName"),
          System.getProperty("http.proxyPassword")));

      int attempt=0;
      boolean retry=false;

      //do{
      //for (attempt = 0; statusCode == -1 && attempt < 3; attempt++) {
        try{
              statusCode =
              clientProc.executeMethod(configProc,method,stateProc); //if
              (statusCode != -1){              //  retry=true;
              //}

        } catch (HttpRecoverableException e) {
                  method.recycle();
                  method.releaseConnection();
                  System.out.println("A recoverable exception occurred,
                  retrying.  " + e.getMessage());        } catch
                  (IOException e) {
                  System.err.println("Failed to download file.");
                  e.printStackTrace();
        }catch (Exception e) {
                  System.err.println("Errorsote.");
                  e.printStackTrace();
        }

      //}while (retry=false);

      if (statusCode == -1) {
        System.err.println("Numero de Intentos:" + attempt);
        System.err.println("Failed to recover from exception.");
      }

      switch (statusCode){
        case 200 :  stateProc.setCookiePolicy(CookiePolicy.RFC2109);
                    Cookie[] cookies =  stateProc.getCookies();
                    System.out.println("Present cookies: ");
                    for (int i = 0; i < cookies.length; i++) {
                      System.out.println(" - " +
                      cookies[i].toExternalForm());                    }
                    urlTC.append(method.getURI().toString());
                    respHTML.append(method.getResponseBodyAsString());
                    break;

        case 302 :  stateProc.setCookiePolicy(CookiePolicy.RFC2109);
                    Cookie[] cookies2 =  stateProc.getCookies();
                    System.out.println("Present cookies: ");
                    for (int i = 0; i < cookies2.length; i++) {
                      System.out.println(" - " +
                      cookies2[i].toExternalForm());                    }
                    Header locationHeader =
                    method.getResponseHeader("location"); if
                    (locationHeader != null) {
                    redirectLocation                    =
                    locationHeader.getValue();
                      urlTC.append(redirectLocation);
                      }else{
                      urlTC.append(method.getURI().toString());
                    }
                    respHTML.append(method.getResponseBodyAsString());
                    break;


      }
      method.recycle();
      method.releaseConnection();

    //} catch (Exception e) {
    //    System.err.println("Failed to download file.");
    //    e.printStackTrace();
    //}
      return (statusCode);

  }


Thanks in advance for your help

Best Regards,
Arturo


Saludos,
Arturo



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org


Mime
View raw message