title: Create 'HTTP Redirect Binding' SAML Request using Java
body: <p>I am trying to implement the same algorithm in Java for SAML HTTP Redirect Binding which is described here: <a href="https://stackoverflow.com/questions/12090403/how-do-i-correctly-prepare-an-http-redirect-binding-saml-request-using-c-sharp">How do I correctly prepare an &#39;HTTP Redirect Binding&#39; SAML Request using C#</a></p>
  
  <p>The algorithm is rather simple:</p>
  
  <ol>
  <li>Build a SAML string</li>
  <li>Compress this string</li>
  <li>Base64 encode the string</li>
  <li>UrlEncode the string.</li>
  </ol>
  
  <p>This should be the equivalent Java algorithm:</p>
  
  <pre><code>    public String encodeRedirectFormat( String samlXML ) throws IOException{
              ByteArrayOutputStream os = new ByteArrayOutputStream();
              DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(os);
              deflaterOutputStream.write( samlXML.getBytes( "UTF-8" ) );
              deflaterOutputStream.close();
              os.close();
              String base64 = Base64.encodeBase64String( os.toByteArray() );
              return URLEncoder.encode( base64, "UTF-8" );
      }
  </code></pre>
  
  <p>I try to encode the simplest assertion:</p>
  
  <pre><code>&lt;saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion"/&gt;
  </code></pre>
  
  <p>This is the output:</p>
  
  <pre><code>eJyzKU7MzTGyciwuTi0qyczPU6jIzckrtgKL2iqVFuVZ5ScWZxZb5SXmphZblSRbBTv6%2BlgZ6RlYJcK0KOnbAQCHfRi3
  </code></pre>
  
  <p>And then try to decode with an online tool like </p>
  
  <p><a href="https://rnd.feide.no/simplesaml/module.php/saml2debug/debug.php" rel="nofollow noreferrer">https://rnd.feide.no/simplesaml/module.php/saml2debug/debug.php</a></p>
  
  <p>the output is invalid. Can someone spot the error? Maybe the Java Deflater works differently?</p>