{"id":1017,"date":"2007-11-14T16:14:13","date_gmt":"2007-11-15T00:14:13","guid":{"rendered":"http:\/\/cephas.net\/blog\/2007\/11\/14\/java-commons-http-client-and-http-proxies\/"},"modified":"2007-11-14T16:14:13","modified_gmt":"2007-11-15T00:14:13","slug":"java-commons-http-client-and-http-proxies","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2007\/11\/14\/java-commons-http-client-and-http-proxies\/","title":{"rendered":"Java, Commons HTTP Client and HTTP proxies"},"content":{"rendered":"<p>If you&#8217;re living at a giant corporation during the day and you want to browse the web you&#8217;re probably going through some sort of proxy to the outside world.  Most of the time you don&#8217;t care, but if you&#8217;re writing a Java application that needs to access resources on the other side of said proxy (ie: the rest of the world), you&#8217;ll eventually end up over <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/guide\/net\/properties.html\">here<\/a>.  That wonderful document will hook you up with all your need to know about setting the proxy host, port and optionally a username and password for your proxy as long as you&#8217;re using <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/net\/URLConnection.html\">URLConnection<\/a>, <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/net\/HttpURLConnection.html\">HttpURLConnection<\/a> or anything that deals with the class <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/net\/URL.html\">URL<\/a>. If you&#8217;re really a go-getter you might even browse over <a href=\"http:\/\/info4tech.wordpress.com\/2007\/05\/04\/java-http-proxy-settings\/\">here<\/a> and read all about how to utilize those properties on the command line, in code or when you&#8217;re deployed inside of Tomcat.<\/p>\n<p>Some of you won&#8217;t be so lucky: you&#8217;ll eventually want to use some <a href=\"http:\/\/wiki.java.net\/bin\/view\/Javawsxml\/RomeFetcher\">advanced tools<\/a> that abstract you away from having to fetch <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/io\/InputStream.html\">InputStreams<\/a> to get your feeds and will instead depend on the <a href=\"http:\/\/jakarta.apache.org\/httpcomponents\/httpclient-3.x\/\">Commons HTTP Client<\/a>, which unfortunately (or fortunately depending on your point of view), doesn&#8217;t care about those nice little system properties that java.net.URL likes and instead goes off and uses <a href=\"http:\/\/svn.apache.org\/viewvc\/jakarta\/httpcomponents\/oac.hc3x\/trunk\/src\/java\/org\/apache\/commons\/httpclient\/protocol\/DefaultProtocolSocketFactory.java?view=markup\">sockets<\/a> directly.  No, instead you have to do something like this:<\/p>\n<pre>HttpClient client = new HttpClient();\r\nHttpConnectionManager conManager = client.getHttpConnectionManager();\r\nclient.getHostConfiguration().setProxy(\"proxyserver.example.com\", 8080);<\/pre>\n<p>and if you want to provide a username and password for said proxy:<\/p>\n<pre>HttpState state = new HttpState();\r\nstate.setProxyCredentials(null, null,\r\n   new UsernamePasswordCredentials(\"username\", \"password\"));\r\nclient.setState(state);<\/pre>\n<p>which is all fine and dandy but sometimes I just wish the world were simpler. Ya know?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re living at a giant corporation during the day and you want to browse the web you&#8217;re probably going through some sort of proxy to the outside world. Most of the time you don&#8217;t care, but if you&#8217;re writing a Java application that needs to access resources on the other side of said proxy &hellip; <a href=\"https:\/\/cephas.net\/blog\/2007\/11\/14\/java-commons-http-client-and-http-proxies\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java, Commons HTTP Client and HTTP proxies<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,32,12],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/1017"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=1017"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/1017\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=1017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=1017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=1017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}