Skip to content

Instantly share code, notes, and snippets.

@maikelsperandio
Created May 2, 2016 12:05
Show Gist options
  • Save maikelsperandio/197647815ef121b31544751f714badfa to your computer and use it in GitHub Desktop.
Save maikelsperandio/197647815ef121b31544751f714badfa to your computer and use it in GitHub Desktop.
package com.maikelsperandio.ssltest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyStore;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
public class Main {
static String jsonLocalAntena(){
StringBuilder json = new StringBuilder();
json.append("{\"codigoLocal\" : \"651\",");
json.append("\"latitude\" : 1.165874651,");
json.append("\"longitude\" : -2.45988465,");
json.append("\"descricaoFaixa\" : \"descrição teste\"}");
return json.toString();
}
static String jsonRegistroPassagem(){
StringBuilder json = new StringBuilder();
json.append("{\"localAntena\" : {\"codigoLocal\":\"651\"},");
json.append("\"descFaixa\" : \"FAIXA1\",");
json.append("\"dhPassagem\" : \"2015-11-17T10:07:47.094-0200\",");
json.append("\"placa\" : \"nrm2951\",");
json.append("\"idClassificacaoPerfil\" : \"per1\",");
json.append("\"idClassePeso\" : \"pes1\",");
json.append("\"protocolo\" : \"j3f20j9gf23j0j3f0923\",");
json.append("\"tara\" : 500.00,");
json.append("\"peso\" : 750.00,");
json.append("\"placaEletronica\" : \"platest\"}");
return json.toString();
}
public static void main(String[] args) {
// registroPassagem();
registraAntena();
}
static void registraAntena(){
try {
// HttpClient httpClient = HttpClientBuilder.create().build();
HttpClientBuilder cb = HttpClientBuilder.create();
SSLContextBuilder sslcb = new SSLContextBuilder();
sslcb.loadTrustMaterial(KeyStore.getInstance(KeyStore.getDefaultType()), new TrustSelfSignedStrategy());
cb.setSslcontext(sslcb.build());
HttpClient httpClient = cb.build();
StringEntity entity = new StringEntity(jsonLocalAntena(), "utf-8");
entity.setContentType("application/json; charset=utf-8");
HttpPost post = new HttpPost("https://xxxdnn3096.locaweb.com.br:8443/cit-monitor-es-web/rest/localAntena/insert");
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
processResponse(response);
System.out.println(response.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
}
static void processResponse(HttpResponse response) throws Exception{
String output;
StringBuilder result = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
while ((output = br.readLine()) != null) {
result.append(output);
}
System.out.println(result.toString());
}
static void registroPassagem(){
try {
// TrustAllCertificates.install();
//
// HttpClient httpClient = HttpClientBuilder.create().build();
HttpClientBuilder cb = HttpClientBuilder.create();
SSLContextBuilder sslcb = new SSLContextBuilder();
sslcb.loadTrustMaterial(KeyStore.getInstance(KeyStore.getDefaultType()), new TrustSelfSignedStrategy());
cb.setSslcontext(sslcb.build());
HttpClient httpClient = cb.build();
StringEntity entity = new StringEntity(jsonRegistroPassagem(), "utf-8");
entity.setContentType("application/json; charset=utf-8");
HttpPost post = new HttpPost("https://xxxdnn3096.locaweb.com.br:8443/cit-monitor-es-web/rest/registroPassagem/insert");
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
System.out.println(response.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment