[Java] Consumir WebService con EasySSL
1 minuto de lecturaCódigo para conectarse y consumir un servicio Soap o Rest en Java SDK 1.6 o 1.8
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import java.io.IOException;
public class Main {
public static void main(String a[]) throws IOException {
testPost();
}
public static void testPost() throws IOException {
System.out.println("llamando post...");
Protocol easyhttps = new Protocol("https", (ProtocolSocketFactory)new EasySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", easyhttps);
HttpClient client = new HttpClient();
PostMethod httppost = new PostMethod("https://wsprod/rest/consulta");
httppost.setRequestEntity(new StringRequestEntity("{\"rutCliente\":\"97080000-K\",\"indicadores\":[\"83\"],\"cuenta\":\"\",\"tipoIndicador\":\"CLI\"}"));
httppost.setRequestHeader("Content-Type", "application/json");
client.executeMethod(httppost);
System.out.println("Status : "+ new String (httppost.getResponseBody()));
}
}
PASS: appdatos.com
Incluye:
commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
commons-httpclient-contrib-ssl-3.1.jar
commons-logging-1.2.jar