// HTTP POST request
public String sendPost(String targetUrl, String parameters) throws Exception {
URL url = new URL(targetUrl);
disableSslVerification();
//HttpURLConnection con = (HttpURLConnection) url.openConnection();
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("charset", "UTF-8");
con.setRequestProperty("Content-Encoding", "gzip");//when gzip
con.setRequestProperty("Accept-Encoding", "gzip");//when gzip
con.setRequestProperty("content-type", "application/x-www-form-urlencoded");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(compress(parameters));
wr.flush();
wr.close();
//non-gzip request
/*OutputStreamWriter outStream = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
PrintWriter writer = new PrintWriter(outStream);
writer.write(parameters.toString());
writer.flush();*/
BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(con.getInputStream()), "UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
댓글 달기