Created
February 12, 2018 12:23
-
-
Save marcgeld/1b57d0642d15d18ed42e1fa9652bf209 to your computer and use it in GitHub Desktop.
HttpPOST xml message to URL
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
| #!/usr/bin/env groovy | |
| import groovy.xml.XmlUtil | |
| import javax.net.ssl.SSLSession | |
| import javax.net.ssl.HostnameVerifier | |
| import javax.net.ssl.SSLContext | |
| import javax.net.ssl.HttpsURLConnection | |
| import static javax.net.ssl.HttpsURLConnection.HTTP_OK | |
| import javax.net.ssl.X509TrustManager | |
| import javax.net.ssl.TrustManager | |
| import java.security.cert.X509Certificate | |
| import java.security.cert.CertificateException | |
| import java.security.NoSuchAlgorithmException | |
| import java.security.KeyManagementException | |
| import java.nio.charset.StandardCharsets | |
| // disableSSLVerification | |
| def trustAllCerts = [ | |
| new X509TrustManager() { | |
| public X509Certificate[] getAcceptedIssuers() { return null } | |
| public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {} | |
| public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {} | |
| } | |
| ] as TrustManager[] | |
| SSLContext sc = SSLContext.getInstance("SSL") | |
| sc.init(null, trustAllCerts, new java.security.SecureRandom()) | |
| HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()) | |
| HostnameVerifier allHostsValid = new HostnameVerifier() { | |
| public boolean verify( String hostname, SSLSession session ) { | |
| println "verify: ${hostname} ${session}" | |
| return true | |
| } | |
| } | |
| HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid) | |
| def iso8601Date = new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")) | |
| def id = UUID.randomUUID().toString() | |
| def url = "https://httpbin.org/post" | |
| def xmlString = """ | |
| <xml> | |
| <id>${id}</id> | |
| <date>${iso8601Date}</date> | |
| </xml> | |
| """ | |
| println "id: ${id} date: ${iso8601Date}" | |
| def xmlOutput = new ByteArrayOutputStream(); | |
| XmlUtil.serialize xmlString, xmlOutput | |
| def post = new URL( url ).openConnection() | |
| post.setHostnameVerifier( new HostnameVerifier() { | |
| public boolean verify(String hostname, SSLSession session) { | |
| return true | |
| } | |
| }) | |
| String encoded = Base64.getEncoder().encodeToString(("user:password").getBytes(StandardCharsets.UTF_8)) | |
| def basicEnc = "Basic ${encoded}" | |
| println "basic ${basicEnc}" | |
| post.setRequestMethod("POST") | |
| post.setDoOutput(true) | |
| post.setRequestProperty("Content-Type", "application/xml") | |
| post.setRequestProperty("Authorization", basicEnc) | |
| post.getOutputStream().write( xmlOutput.toByteArray() ) | |
| def responseCode = post.getResponseCode() | |
| def responseMessage = post.getResponseMessage() | |
| println "http error code: ${responseCode} - ${responseMessage}" | |
| if( responseCode.equals( HTTP_OK ) ) { | |
| println post.getHeaderField("Content-Type") | |
| println( post.getInputStream().getText() ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment