Last active
March 27, 2019 15:34
-
-
Save marcgeld/7eec3660f2c5683a191e230c861ae027 to your computer and use it in GitHub Desktop.
Post to httpbin.org/post
This file contains 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("Accept", "application/xml") | |
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