gzip Java Request and Response
To implement both gzip request and response, using Java, use the following code. For unannotated code, click here.
|
package com.cendant.tds.samples; |
|
|
|
import java.io.DataInputStream; |
|
import java.io.DataOutputStream; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.net.URL; |
|
import java.net.URLConnection; |
|
import java.util.zip.GZIPInputStream; |
|
import java.util.zip.GZIPOutputStream; |
|
|
|
import sun.misc.BASE64Encoder; |
|
|
|
public class GZipSample { |
|
public static void main(String[] args) throws IOException { |
|
BASE64Encoder b64 = new BASE64Encoder(); |
|
URL url; |
|
URLConnection urlConn; |
|
DataOutputStream printout; |
|
InputStream input = null; |
Create a new URL OBJECT and open the connection. Ensure URL is specific to your region or service level. |
url
= new URL("https://americas.webservices.travelport.com/B2BGateway/ |
|
urlConn = url.openConnection(); |
|
urlConn.setDoInput(true); |
|
urlConn.setDoOutput(true); |
|
urlConn.setUseCaches(false); |
|
urlConn.setRequestProperty("Content-Type","text/xml"); |
Let the server know that you are sending a gzipped request |
urlConn.setRequestProperty("Content-Encoding","gzip"); //Let the server know that you are sending gzipped request |
|
urlConn.setRequestProperty("SOAPAction","echoservice"); |
Let the server know that you can process the gzipped response. |
urlConn.setRequestProperty("Accept-Encoding","gzip"); //Let the server know that you can process gzipped response |
Update this with your User ID and Password |
String auth = b64.encode("USERID:PASSWORD".getBytes()); //Update with your own userID and password |
|
urlConn.setRequestProperty("Authorization", "Basic " + auth); |
Create a gzipped stream for output |
printout = new DataOutputStream(new GZIPOutputStream(urlConn.getOutputStream())); //create a gzipped stream for output |
|
|
Put your request body in the SOAP Envelope |
String content = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <SOAP-ENV:Body> PUT YOUR REQUEST BODY HERE </SOAP-ENV:Body></SOAP-ENV:Envelope>"; |
Write your request to the gzipped output stream |
printout.writeBytes(content); // write your request to the gzipped output stream |
|
printout.flush(); |
|
printout.close(); |
|
|
|
String encoding = urlConn.getContentEncoding(); |
|
try |
|
{ |
Check whether the server sent a gzipped response |
if (encoding != null && encoding.equalsIgnoreCase("gzip")) // Check if the server sent you a gzipped response |
|
{ |
Process the gzipped response |
System.out.println("THIS IS A GZIPPPPPPPPPED STREAM"); // Process gzipped response |
|
input = new GZIPInputStream(urlConn.getInputStream()); |
|
} |
|
else |
|
{ |
|
System.out.println("THIS IS NOT A GZIPPPPPPPPPED STREAM"); |
|
input = new DataInputStream(urlConn.getInputStream()); |
|
} |
|
} |
|
catch (Exception e) |
|
{ |
|
} |
|
|
|
int rc; |
|
|
|
// Read from the stream and process the response |
Read from the stream and process the response |
while (-1 != (rc = input.read())) { |
|
System.out.print((char) rc); |
|
} |
|
input.close(); |
|
|
|
|
|
} |
|
} |