HttpCient Connection - dauert zur lange!

yigiter

Mitglied
Hallo zusammen,

ich habe folgendes Problem...

Eine Gruppe von Threads versuchen mit Http Connection (Libary HttpClient Jakarta Commons) den Inhalt einer Webseite auszulesen. Leider dauert die Anfrage 20 sek. woran kann das liegen...

Ich dachte es hat etwas mit dem Header zu tun.. jedoch diese Einstellungen haben auch nichts gebracht.

Code:
    InputStream tInputStream = null;
		// Create an instance of HttpClient.
		HttpClientParams tClientParams = new HttpClientParams();

		tClientParams.setParameter(HttpClientParams.SO_TIMEOUT, new Integer(5000));
		tClientParams.setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(10000));
		tClientParams.setParameter(HttpMethodParams.USER_AGENT,
				"Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9) Gecko/2008052906 Firefox/3.0");
		tClientParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
		tClientParams.setContentCharset("ISO-8859-1");

		HttpClient tHttpClient = new HttpClient(tClientParams);

		tHttpClient.getParams().setVersion(HttpVersion.HTTP_1_1);
		tHttpClient.getParams().setHttpElementCharset("US-ASCII");
		tHttpClient.getParams().setContentCharset("ISO-8859-1");
		tHttpClient.getParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);

		// Create a method instance.
		GetMethod method = new GetMethod(aUrl);
		// Provide custom retry handler is necessary
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
		// Warnings sollen Ignoriert werden
		method.getParams().setParameter(HttpClientParams.WARN_EXTRA_INPUT, false);
		method.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		method.addRequestHeader("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
		method.addRequestHeader("Connection", "Keep-Alive");
		method.addRequestHeader("Keep-Alive", "300");

		String tInseratText = "";

		try {
			// Execute the method.
			int statusCode = tHttpClient.executeMethod(method);

			if (statusCode != HttpStatus.SC_OK) {
				// Error
			}

			tInputStream = method.getResponseBodyAsStream();
			if (tInputStream != null) {
				BufferedReader in = new BufferedReader(new InputStreamReader(tInputStream, CHARSET_UTF_8));
				
// TODO ...

				
				
				/* Reader wiederschliessen */
				in.close();
			}
		} catch (HttpException e) {
			//
		} catch (IOException e) {
			//
		} finally {
			// Release the connection.
			method.releaseConnection();
		}

MfG
Yigiter
 
Eine Exception fliegt nicht!

Also ich kann mir die Zeiten nicht erklären, wenn das Programm auf einem "richitgen" Server (also kein V-Server) läuft benötigt dieser ca. 18000ms und mit einer DSL 2000 leitung 1680ms ?

Gruß
Yigiter
 
Hallo,

vielleicht ist der DNS Lookup zu lahm. Sprich die URL doch mal nur mit der IP Adresse an.
Ansonsten kann es sein, dass der Server der Seite die du aufrufen willst deine Host Adresse auflösen will (RemoteHost) um deinen Zugriff zu loggen - dass kann dan schon mal unterschiedlich lange dauern.

Gruß Tom
 
Hallo,

gibt es eine Alternative zu dem Http Connection (Libary HttpClient Jakarta Commons)!

Kann die Libary die Ursache sein!?

Gruß
Yigiter
 
Hallo zusammen,

ich habe nun folgenden Code auf dem Server ausgeführt..

Code:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * An example that performs GETs from multiple threads.
 * 
 * @author Michael Becke
 */
public class MultiThreadedExample {

    /**
     * Constructor for MultiThreadedExample.
     */
    public MultiThreadedExample() {
        super();
    }

    public static void main(String[] args) {
        
        // Create an HttpClient with the MultiThreadedHttpConnectionManager.
        // This connection manager must be used if more than one thread will
        // be using the HttpClient.
        HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        // Set the default host/protocol for the methods to connect to.
        // This value will only be used if the methods are not given an absolute URI
        httpClient.getHostConfiguration().setHost("hc.apache.org", 80, "http");
        
        // create an array of URIs to perform GETs on
        String[] urisToGet = {
            "/",
            "/httpclient-3.x/status.html",
            "/httpclient-3.x/methods/",
            "http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/"
        };
        
        // create a thread for each URI
        GetThread[] threads = new GetThread[urisToGet.length];
        for (int i = 0; i < threads.length; i++) {
            GetMethod get = new GetMethod(urisToGet[i]);
            get.setFollowRedirects(true);
            threads[i] = new GetThread(httpClient, get, i + 1);
        }
        
        // start the threads
        for (int j = 0; j < threads.length; j++) {
            threads[j].start();
        }
        
    }
    
    /**
     * A thread that performs a GET.
	 */
    static class GetThread extends Thread {
        
        private HttpClient httpClient;
        private GetMethod method;
        private int id;
        
        public GetThread(HttpClient httpClient, GetMethod method, int id) {
            this.httpClient = httpClient;
            this.method = method;
            this.id = id;
        }
        
        /**
         * Executes the GetMethod and prints some satus information.
         */
        public void run() {
            
            try {
                
                System.out.println(id + " - about to get something from " + method.getURI());
                // execute the method
                httpClient.executeMethod(method);
                
                System.out.println(id + " - get executed");
                // get the response body as an array of bytes
                byte[] bytes = method.getResponseBody();
                
                System.out.println(id + " - " + bytes.length + " bytes read");
                
            } catch (Exception e) {
                System.out.println(id + " - error: " + e);
            } finally {
                // always release the connection after we're done 
                method.releaseConnection();
                System.out.println(id + " - connection released");
            }
        }
       
    }
    
}

Es kommt eine Exception!

Code:
Aug 2, 2008 3:03:24 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused
Aug 2, 2008 3:03:24 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request

Nach 2 min nocheinmal ausgeführt.. nun kommt keine Exception !

Gruß
Yigiter
 
Zuletzt bearbeitet:
Zurück