Last active
February 23, 2018 16:52
-
-
Save nenodias/dae4715c2ff3b5ab8ec3c31c0473431b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.InputStreamReader; | |
| import java.io.OutputStream; | |
| import java.net.HttpURLConnection; | |
| import java.net.URL; | |
| public class SoapLite { | |
| @SuppressWarnings("restriction") | |
| private static void setKeytoreOnSystem(String localPFX, String senha) { | |
| System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true"); | |
| System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol"); | |
| java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); | |
| System.setProperty("javax.net.ssl.keyStoreType", "PKCS12"); | |
| System.setProperty("javax.net.ssl.keyStore", localPFX); | |
| System.setProperty("javax.net.ssl.keyStorePassword", senha); | |
| System.setProperty("javax.net.ssl.trustStoreType", "JKS"); | |
| System.setProperty("javax.net.ssl.trustStore", CACERTS); | |
| System.setProperty("javax.net.ssl.trustStorePassword", "changeit"); | |
| } | |
| private static File getArquivoCacerts(){ | |
| String pastaTemporaria = System.getProperty("java.io.tmpdir"); | |
| String caminhoArquivoCacerts = pastaTemporaria.concat("/").concat(NOME_ARQUIVO_CACERTS); | |
| InputStream inputStreamCacerts = Main.class.getResourceAsStream("/cacerts/" + NOME_ARQUIVO_CACERTS); | |
| File arquivoCacerts = null; | |
| if(inputStreamCacerts != null){ | |
| try{ | |
| arquivoCacerts = new File(caminhoArquivoCacerts); | |
| OutputStream outputStream = new FileOutputStream(arquivoCacerts); | |
| int read = 0; | |
| byte[] bytes = new byte[1024]; | |
| while ((read = inputStreamCacerts.read(bytes)) != -1) { | |
| outputStream.write(bytes, 0, read); | |
| } | |
| inputStreamCacerts.close(); | |
| outputStream.flush(); | |
| outputStream.close(); | |
| }catch(Exception e){ | |
| e.printStackTrace(); | |
| } | |
| } | |
| return arquivoCacerts; | |
| } | |
| private String requestWs(String xml,String url,String metodo) throws Exception { | |
| URL urlGo = new URL(url); | |
| HttpURLConnection con = (HttpURLConnection) urlGo.openConnection(); | |
| con.setRequestProperty("Content-type", "text/xml; charset=UTF-8"); | |
| con.setRequestProperty("SOAPAction", "http://nfse.goiania.go.gov.br/ws/" + metodo); | |
| con.setRequestMethod("POST"); | |
| con.setDoOutput(true); | |
| con.setDoInput(true); | |
| OutputStream reqStream = con.getOutputStream(); | |
| reqStream.write(xml.getBytes("UTF-8")); | |
| StringBuilder xmlRetorno = new StringBuilder(); | |
| String line; | |
| BufferedReader retornoWs = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
| while ((line = retornoWs.readLine()) != null) { | |
| xmlRetorno.append(line); | |
| } | |
| retornoWs.close(); | |
| con.disconnect(); | |
| return xmlRetorno.toString(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Teste { | |
| public static void main(String[] args) throws IOException, SOAPException { | |
| String xmlInput = " <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://litwinconsulting.com/webservices/\">\n" | |
| + " <soapenv:Header/>\n" | |
| + " <soapenv:Body>\n" | |
| + " <web:RES>\n" | |
| + " <web:RETURNCODE>100 </web:RETURNCODE> \n" | |
| + " </web:RES>\n" | |
| + " <web:GetWeather>\n" | |
| + " <!--Optional:-->\n" | |
| + " <web:City>%CITY%</web:City>\n" | |
| + " </web:GetWeather>\n" | |
| + " </soapenv:Body>\n" | |
| + " </soapenv:Envelope>"; | |
| MessageFactory factory = MessageFactory.newInstance(); | |
| SOAPMessage message = factory.createMessage( | |
| new MimeHeaders(), | |
| new ByteArrayInputStream(xmlInput.getBytes(Charset | |
| .forName("UTF-8")))); | |
| SOAPBody body = message.getSOAPBody(); | |
| NodeList returnList = body.getElementsByTagName("web:RES"); | |
| boolean isSuccess = false; | |
| for (int k = 0; k < returnList.getLength(); k++) { | |
| NodeList innerResultList = returnList.item(k).getChildNodes(); | |
| for (int l = 0; l < innerResultList.getLength(); l++) { | |
| if (innerResultList.item(l).getNodeName() | |
| .equalsIgnoreCase("web:RETURNCODE")) { | |
| isSuccess = Integer.valueOf(innerResultList.item(l) | |
| .getTextContent().trim()) == 100 ? true : false; | |
| } | |
| } | |
| } | |
| if (isSuccess) { | |
| NodeList list = body.getElementsByTagName("web:GetWeather"); | |
| for (int i = 0; i < list.getLength(); i++) { | |
| NodeList innerList = list.item(i).getChildNodes(); | |
| for (int j = 0; j < innerList.getLength(); j++) { | |
| System.out.println(innerList.item(j).getNodeName()); | |
| System.out.println(innerList.item(j).getTextContent()); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment