Skip to content

Instantly share code, notes, and snippets.

@pvillard31
Created May 9, 2016 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvillard31/9eee37f423a334fd1006d4d15f731330 to your computer and use it in GitHub Desktop.
Save pvillard31/9eee37f423a334fd1006d4d15f731330 to your computer and use it in GitHub Desktop.
package test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPOutputStream;
public class Test {
public static void main(String[] args) throws Exception {
//set up connection
URL url = new URL("http://localhost:4444/test");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// Send post request
conn.setRequestMethod("POST");
conn.setReadTimeout(300000);
conn.setConnectTimeout(300000);
conn.setRequestProperty("Content-Type","application/gzip");
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
GZIPOutputStream gzip = new GZIPOutputStream(wr);
String data = createDataSize(100000);
gzip.write(data.getBytes());
gzip.flush();
gzip.close();
int code = conn.getResponseCode();
System.out.println(code);
//System.out.println(data.equals(getStringFromInputStream(conn.getInputStream())));
// Thread.sleep(15000);
conn.disconnect();
}
/**
* Creates a message of size @msgSize in KB.
*/
private static String createDataSize(int msgSize) {
// Java chars are 2 bytes
msgSize = msgSize/2;
msgSize = msgSize * 1024;
StringBuilder sb = new StringBuilder(msgSize);
for (int i=0; i<msgSize; i++) {
sb.append('a');
}
return sb.toString();
}
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment