I think the HttpClient essentially use the urlconnection, then i would
ask why httpclient will faster than urlconnection?
I think the benefit of HttpClient is that it manage the connection, that
make your code more neat, you can focus on the business logic...
Hi,
I have a problem when trying to comparing the speed between JDK
HttpURLConnection and HttpClient.
Now I have two methods, one is performed with jdk(1.5) httpurlConnection,
the other is performed with httpClient( 3.0.1). Both of them try to
download
the same
webpage(http://jakarta.apache.org).After<http://jakarta.apache.org%29.After>testing,
the average time which performed JDK httpurlconnection is 200ms,
so as the one which performed httpclient.That means their speeds are
almost
the same.
So, since HttpClient use persistence connection default but JDK do
not, why
httpClient's speed could not be more fast than JDK?
Another problem is, is it normal to httpclient to use 200ms to download a
page like http://jakarta.apache.org? I mean, when using persistence
connection, maybe this could not be accepted.
Any help will be great appreciated
----------------------------------------Source to httpclient
method-------------------------------------
public void testGetMethod(){
MultiThreadedHttpConnectionManager manager=new
MultiThreadedHttpConnectionManager();
manager.getParams().setConnectionTimeout(3000);
manager.getParams ().setDefaultMaxConnectionsPerHost(1);
// manager.getParams().setMaxTotalConnections(30);
HttpClient httpClient = new HttpClient(manager);
// 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().setProxy("192.168.0.10",80);
// httpClient.getHostConfiguration().setHost("jakarta.apache.org",
80, "http");
GetMethod method = new GetMethod(" http://jakarta.apache.org");
for (int i=0;i<10;i++){
try {
// System.out.println(" - about to get something from " +
method.getURI());
// execute the method
long start=System.currentTimeMillis();
httpClient.executeMethod(method);
// System.out.println(" - get executed");
// get the response body as an array of bytes
byte[] bytes = method.getResponseBody();
System.out.println("cost time is "+(System.currentTimeMillis
()-start));
// System.out.println(new String(bytes));
// System.out.println(" - " + bytes.length + " bytes read");
} catch (Exception e) {
System.out.println(" - error: " + e);
} finally {
// always release the connection after we're done
method.releaseConnection();
// System.out.println(" - connection released");
}
}
}
------------------------------------------source code to JDK
httpurlconnection------------------------------------------------
public void testJDKHttpURlconnection() {
for (int i = 0; i < 10; i++) {
try {
long start = System.currentTimeMillis();
URL requestedURL = new URL("http://jakarta.apache.org");
HttpURLConnection conn = (HttpURLConnection) requestedURL
.openConnection();
conn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0
)");
conn.setUseCaches(false);
conn.connect();
BufferedInputStream remoteBIS = new
BufferedInputStream(conn
.getInputStream());
ByteArrayOutputStream baos = new
ByteArrayOutputStream(10240);
byte[] buf = new byte[1024];
int bytesRead = 0;
while (bytesRead >= 0) {
baos.write(buf, 0, bytesRead);
bytesRead = remoteBIS.read(buf);
}
byte[] content = baos.toByteArray();
System.out.println("cost time is "
+ (System.currentTimeMillis() - start));
} catch (Exception e) {
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]