Hi,
Before bothering you with my question, I feel I have to introduce myself.
My name is Jeremy Lardon and I am a PhD student in France (it explains
my poor english).
For my research work, I rely on httpclient to request pages.
But - if there wouldn't be a "but", I won't bother you with another mail
on the list - I can't manage to request a page through the proxy of my
university.
To be more precise, the context is the following :
The version of HttpClient I use is the 4.0-alpha2 with dependencies.
The proxy used by my university is squid/2.6.STABLE17-20071206.
I join the java file I used as test (example from
http://hc.apache.org/httpcomponents-client/examples.html, I modifed to
match 4.0-alpha2) and the exception stack trace.
Any help will be greatly welcomed.
Best regards,
--
Jérémy Lardon
Laboratoire DIOM, équipe SATIn - Doctorant
+33 4 77 48 50 34
/*
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package testClient;
import java.net.URISyntaxException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
/**
* How to send a request via proxy using [EMAIL PROTECTED] HttpClient HttpClient}.
*
* @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
*
*
* <!-- empty lines above to avoid 'svn diff' context problems -->
* @version $Revision$
*
* @since 4.0
*/
public class ClientExecuteProxy {
/**
* The default parameters.
* Instantiated in [EMAIL PROTECTED] #setup setup}.
*/
private static HttpParams defaultParameters = null;
/**
* The scheme registry.
* Instantiated in [EMAIL PROTECTED] #setup setup}.
*/
private static SchemeRegistry supportedSchemes;
/**
* Main entry point to this example.
*
* @param args ignored
*/
public final static void main(String[] args)
throws Exception {
// make sure to use a proxy that supports CONNECT
final HttpHost proxy =
new HttpHost("<proxy address>", 3128, "http");
setup(); // some general setup
HttpClient client = createHttpClient();
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpUriRequest req = createRequest();
System.out.println("executing request to "+req.getURI()+" via " + proxy);
HttpEntity entity = null;
try {
HttpResponse rsp = client.execute(req);
entity = rsp.getEntity();
System.out.println("----------------------------------------");
System.out.println(rsp.getStatusLine());
Header[] headers = rsp.getAllHeaders();
for (int i=0; i<headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
if (rsp.getEntity() != null) {
System.out.println(EntityUtils.toString(rsp.getEntity()));
}
}
catch (Exception e) {
System.err.println(e.getClass().getSimpleName()+" : "+e.getMessage());
e.printStackTrace(System.err);
}
finally {
// If we could be sure that the stream of the entity has been
// closed, we wouldn't need this code to release the connection.
// However, EntityUtils.toString(...) can throw an exception.
// if there is no entity, the connection is already released
if (entity != null)
entity.consumeContent(); // release connection gracefully
}
} // main
private final static HttpClient createHttpClient() {
ClientConnectionManager ccm =
new ThreadSafeClientConnManager(getParams(), supportedSchemes);
DefaultHttpClient dhc =
new DefaultHttpClient(ccm, getParams());
return dhc;
}
/**
* Performs general setup.
* This should be called only once.
*/
private final static void setup() {
supportedSchemes = new SchemeRegistry();
// Register the "http" and "https" protocol schemes, they are
// required by the default operator to look up socket factories.
SocketFactory sf = PlainSocketFactory.getSocketFactory();
supportedSchemes.register(new Scheme("http", sf, 80));
sf = SSLSocketFactory.getSocketFactory();
supportedSchemes.register(new Scheme("https", sf, 443));
// prepare parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
defaultParameters = params;
} // setup
private final static HttpParams getParams() {
return defaultParameters;
}
/**
* Creates a request to execute in this example.
*
* @return a request without an entity
*/
private final static HttpUriRequest createRequest() {
HttpUriRequest req = null;
try {
req = new HttpGet("http://www.google.fr");
} catch (URISyntaxException e) {
e.printStackTrace();
}
return req;
}
} // class ClientExecuteProxy
org.apache.http.conn.HttpHostConnectException: Connection to
http://www.google.fr refused
at
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:138)
at
org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
at
org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
at
org.apache.http.impl.client.DefaultClientRequestDirector.execute(DefaultClientRequestDirector.java:283)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:524)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:476)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:435)
at testClient.ClientExecuteProxy.main(ClientExecuteProxy.java:114)
Caused by: java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at
org.apache.http.conn.PlainSocketFactory.connectSocket(PlainSocketFactory.java:110)
at
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:134)
... 7 more
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]