Send POST request to GSA CB with Java
Hey.
I'm using this GSA CB with Java.
And i trying to use HttpURLConnection to send a POST request to http://127.0.0.1:80/gsa_test.gsa and hope to get response with String like
The solution is :: hyby7.
Following is my code:
private void sendPost() throws Exception {
String url = "http://127.0.0.1:80/gsa_test.gsa";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "source_url=http://downloads.ziddu.com/CaptchaSecurityImages.php?width=100&height=38&characters=5&" +
"captcha_plattform=any";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Sending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
//get Response
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response);
}
I suppose i should get answer at response.
But the response is always empty.
I do receive responseCode=200 by con.getResponseCode();
Did i missing any parameter or else.
Thx for helping.
Best regards.
Comments