WebDev: Usando diversos comandos e linguagens para buscar conteúdo via HTTP

= Dica =

Use o plugin Postman do Google Chrome para gerar trechos de código na sua linguagem preferida!!!

Ficheiro:PostMan01.jpg

= Exemplos =

cURL
curl -X POST -d "fizz=buzz" http://httpbin.org/ip

Python (with Requests)
import requests, time r = requests.post('http://httpbin.org/ip', data={"ts":time.time}) print r.status_code print r.content

Node.js (with request)
var request = require('request'); var url ='http://httpbin.org/ip' request(url, function (error, response, body) {  if (!error) {     console.log(body);   } });

Perl
use LWP::Simple; $contents = get("http://httpbin.org/ip");

Java (Apache Commons)
Importante: Apache Commons foi substituído pelo Apache HttpComponents e seus módulos HttpClient e HttpCore.

import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; import org.apache.commons.httpclient.params.HttpMethodParams; import java.io.*; public class JavaHttpClientExample { public static void main(String[] args) { HttpClient client = new HttpClient; GetMethod method = new GetMethod("http://httpbin.org/ip"); try { int statusCode = client.executeMethod(method); byte[] responseBody = method.getResponseBody; System.out.println(new String(responseBody)); } catch (Exception e) { System.err.println("Fatal error: " + e.getMessage); e.printStackTrace; } finally { method.releaseConnection; }  } }

PHP


C# / .NET (4.5+)
using System; using System.Net.Http; using System.Threading.Tasks; namespace NetHttpClientExample {  class Program {    static void Main(string[] args) {      var task = MakeRequest; task.Wait; var response = task.Result; var body = response.Content.ReadAsStringAsync.Result; Console.WriteLine(body); }    private static async Task MakeRequest {      var httpClient = new HttpClient; return await httpClient.GetAsync(new Uri("http://httpbin.org/ip")); }  } }

= Ver também =


 * Mais Artigos sobre Linux / UNIX / AIX
 * Mais Artigos sobre Cloud / WebDev / Tecnologias