This is how my code looks like.
val proxyHost = new HttpHost("myProxyHost.com", 8080, "https")
val httpClient = HttpClients.custom
.useSystemProperties()
.setConnectionManager(new PoolingHttpClientConnectionManager)
.setRedirectStrategy(TrackingRedirectStrategy)
.setProxy(proxyHost)
.build()
val httpContext = new BasicHttpContext
val httpGet = new HttpGet(uri.toASCIIString)
val response = httpClient.execute(httpGet, httpContext)
But the request still goes through without using the myProxyHost.com proxy. I don't understand what I am doing wrong or where is my setProxy config getting overwritten.
(This is scala code, using the standard apache httpclient library.)
I ran your code using a dummy proxy httpServer that always returns a string constant. The only difference being that I used a different redirect strategy. Seems everything is alright.
class SimpleHttpServer extends Thread {
private val threadPoolExecutor: ExecutorService = Executors.newFixedThreadPool(10)
private val server = HttpServer.create(new InetSocketAddress("localhost", 8001), 0);
server.createContext("/", new DefaultHandler());
server.setExecutor(threadPoolExecutor);
override def run(): Unit = {
server.start()
}
}
class DefaultHandler extends HttpHandler {
override def handle(httpExchange: HttpExchange): Unit = {
httpExchange.sendResponseHeaders(200, 5)
httpExchange.getResponseBody.write("hello".getBytes())
httpExchange.getResponseBody.flush()
httpExchange.getResponseBody.close()
}
}
object SimpleHttpServer extends InputReader {
def main(args: Array[String]): Unit = {
val server = new SimpleHttpServer
server.start()
val proxyHost = new HttpHost("localhost", 8001, "http")
val httpClient = HttpClients.custom
.useSystemProperties()
.setConnectionManager(new PoolingHttpClientConnectionManager)
.setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
.setProxy(proxyHost)
.build()
val httpContext = new BasicHttpContext
val httpGet = new HttpGet("http://www.google.com")
val response = httpClient.execute(httpGet, httpContext)
println(readRecur(response.getEntity.getContent))
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.