ptimson
ptimson

Reputation: 5843

Java bypass proxy programmatically apache

Background

I am using AWS Java SDK for SQS to read a message from a local queue. I am using ElasticMQ for my local queue.

@Test
public void readMessageFromQueue() {

    AWSCredentialsProviderChain credentialsProvider = new AWSCredentialsProviderChain(new DefaultAWSCredentialsProviderChain());
    ClientConfiguration clientConfiguration = new ClientConfiguration();

    AmazonSQSClient sqsClient = new AmazonSQSClient(credentialsProvider, clientConfiguration);
    sqsClient.setEndpoint("http://127.0.0.1:9324/queue");
    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest("queue1").withMaxNumberOfMessages(10);

    ReceiveMessageResult receiveMessageResult = sqsClient.receiveMessage(receiveMessageRequest);
    List<Message> sqsMessages = receiveMessageResult.getMessages();

}

This works perfectly until I am connected to my internet via proxy (at work)

I then get a 504 as it tries to route my localhost via my proxy.

Question

How do I bypass my proxy for my localhost programmatically in Java?

I have tried the following to no avail

System.setProperty("NO_PROXY", "127.0.0.1");
System.setProperty("proxySet", "false");
System.setProperty("proxyHost", "");
System.setProperty("proxyPort", "");
System.setProperty("no_proxy", "127.0.0.1");
System.setProperty("http.no_proxy", "127.0.0.1");
System.setProperty("http.proxySet", "false");

The only thing that works is adding 127.0.0.1 to bypass proxy in Mac Network Settings.

localhost in bypass proxy

I have also tried localhost (oppose to 127.0.0.1)

Any Ideas?

Upvotes: 1

Views: 1295

Answers (1)

user4686046
user4686046

Reputation: 4881

Have you tried setting http.nonProxyHosts?

String nonProxyHosts = System.getProperty("http.nonProxyHosts");
nonProxyHosts = nonProxyHosts == null ? "127.0.0.1" : nonProxyHosts + "|127.0.0.1";
System.setProperty("http.nonProxyHosts", nonProxyHosts);

Upvotes: 3

Related Questions