1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
ProxyClient proxyclient = new ProxyClient();
// set the host the proxy should create a connection to
//
// Note: By default port 80 will be used. Some proxies only allow
// conections
// to ports 443 and 8443. This is because the HTTP CONNECT method was
// intented
// to be used for tunneling HTTPS.
proxyclient.getHostConfiguration().setHost("www.yahoo.com");
// set the proxy host and port
proxyclient.getHostConfiguration().setProxy("adresseProxy",
8080);
// set the proxy credentials, only necessary for authenticating proxies
proxyclient.getState().setProxyCredentials(
new AuthScope("adresseProxy", 8080, null),
new UsernamePasswordCredentials("monLogin", "monMotdepasse"));
// create the socket
ProxyClient.ConnectResponse response = proxyclient.connect();
if (response.getSocket() != null) {
Socket socket = response.getSocket();
try {
// go ahead and do an HTTP GET using the socket
Writer out = new OutputStreamWriter(socket.getOutputStream(),
"ISO-8859-1");
out.write("GET http://www.yahoo.com/ HTTP/1.1\r\n");
out.write("Host: www.yahoo.com\r\n");
out.write("Agent: whatever\r\n");
out.write("\r\n");
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream(), "ISO-8859-1"));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} finally {
// be sure to close the socket when we're done
socket.close();
}
} else {
// the proxy connect was not successful, check connect method for
// reasons why
System.out.println("Connect failed: "
+ response.getConnectMethod().getStatusLine());
System.out.println(response.getConnectMethod()
.getResponseBodyAsString());
} |
Partager