Multiple proxies handling
Hi,
I have an application with multiple threads that run simultaneously. Each thread has its own HttpURLConnection object. This object will send a http request to a webserver. I would like to be able to specify the proxy that this particular request will go thru before arriving to the origin webserver. In other words, the proxy setting is localized for each thread. Therefore, I don't want to use the system properties to set the proxy. It looks to me that HttpURLConnection doesn't have this feature. Is it possible to do it with the current JVM? If not, is there a workaround?
Any recommendation would be greatly appreciated.
Sincerely,
[674 byte] By [
tbtong] at [2007-9-26 1:20:42]

Hi,
I've found a complete http client library which allows setting the proxy per connection and globally. For more information, see
http://www.innovation.ch/java/HTTPClient/advanced_info.html#proxies
for download, see
http://www.innovation.ch/java/HTTPClient/
Hope this helps,
Kurt.
Hi Kurt,
Thanks for the recommendation. It seems very promising. I'm trying to build a prototype to make sure that it works for me by creating a simple test program. However, I'm having some problem with it. Below is my code:
public static void main(String[] args) {
BufferedReader input;
CookieModule.setCookiePolicyHandler(null);
try
{
URL url = new URL("http://w3.internal.com/cf/home/news/news_detail.cfm?article_rid=647");
HTTPConnection con = new HTTPConnection(url);
NVPair[] def_hdrs = { new NVPair("Connection", "close") };
con.setDefaultHeaders(def_hdrs);
String file = url.getFile();
HTTPResponsersp = con.Get(file);
int statusCode = rsp.getStatusCode();
if (statusCode >= 300)
{
System.err.println("Received Error: "+rsp.getReasonLine());
System.err.println(rsp.getText());
}
else
System.out.println(rsp.getText());
}
}
catch (IOException ioe)
{
System.err.println(ioe.toString());
}
catch (ParseException pe)
{
System.err.println("Error parsing Content-Type: " + pe.toString());
}
catch (ModuleException me)
{
System.err.println("Error handling request: " + me.getMessage());
}
}
The program generated java.io.EOFException: Premature EOF encountered and didn't print out the response content as wanted. This behavior is not very consistent since I also tried others different urls and some work, some don't.
Have you used this lib? Have you ever seen this behavior? What is the cause? Am I missing something?
Thanks in advance for your help.
Hi,
Unfortunately, I've never used the package before (that is, not until today). I did a little searching on the internet about your question and ran into this package - believing this was what you're looking for.
I've tried your code with multiple url's and didn't receive any errors so far. Can you try to find some similarities between the url's which are failing? It might be possible that a certain http feature is not yet implemented or the code not being bugfree. I think however the code can be a good starting point for you.
It can also be helpful if you print the stack trace after the exception occurred - this would give more information about where the error occurred initially.
You can also post the error to the email address mentionned on the site. Maybe they will be able to help you.
Regards,
Kurt.