Skip to content

Instantly share code, notes, and snippets.

@azumamagus
Created January 18, 2019 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azumamagus/4e4468cc1b488fa96f7f840405cbdd5d to your computer and use it in GitHub Desktop.
Save azumamagus/4e4468cc1b488fa96f7f840405cbdd5d to your computer and use it in GitHub Desktop.
WebClient
package br.com.teste.agenda;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class WebClient {
public String post(String json){
try {
URL url = new URL("https://www.caelum.com.br/mobile");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-type", "application/json");
connection.setDoInput(true);
connection.setDoOutput(true);
PrintStream saida = new PrintStream(connection.getOutputStream());
saida.println(json);
connection.connect();
String resposta = new Scanner(connection.getInputStream()).next();
return resposta;
} catch (Exception e){
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment