Originally posted by Radhika Reddy:
I need to send POST Request to System written in CGI. Then its going to send HTTP Response to me. Here is the code.
StringBuffer resultsArea =new
StringBuffer("");
int responseCode=0;
String responseMsg = null;
String dawgsHost = configLocator.getDawgsHost();
String protocol = "http";
String path = "/RADS";
String sudId = "1.2.3"; //"1.2.3";//"235.12.1"; //(String)hmap.get("SudId");
String channelId = "1"; //(String)hmap.get("channelId");
String strategyId = "1"; //(String)hmap.get("StrategyId");
String radStatus= "ON"; //(String)hmap.get("status");
String username = "zzzz"; //(String)hmap.get("username");
String remedyTicketId = "qwertty345gtre"; //(String)hmap.get("remedyTicketId");
String slowdown = (String)hmap.get("slowdown");
String urlString = protocol+"://"+dawgsHost+path;
String postData = URLEncoder.encode("action")+"="+ URLEncoder.encode("update_rollout_strategy")+"&"+ URLEncoder.encode("channel_id")+"="+URLEncoder.encode(channelId)+"&"+ URLEncoder.encode("sud_id")+"="+URLEncoder.encode(sudId)+"&"+ URLEncoder.encode("strategy_id")+"="+URLEncoder.encode(strategyId)+"&"+ URLEncoder.encode("rad_status")+"="+URLEncoder.encode(radStatus)+"&"+ URLEncoder.encode("user_screen_name")+"="+URLEncoder.encode(username)+"&"+ URLEncoder.encode("remedy_ticket_id")+"="+URLEncoder.encode(remedyTicketId)+"&"+ URLEncoder.encode("slowdown_percent")+"="+URLEncoder.encode(slowdown);
try{
URL url = new URL(urlString);
HttpURLConnection connection = (
HttpURLConnection)url.openConnection();
// Make sure browser doesn't cache this URL.
connection.setUseCaches(false);
// Tell browser to allow me to send data to server.
connection.setDoOutput(true);
ByteArrayOutputStream byteStream =
new
ByteArrayOutputStream(512); // Grows if necessary
PrintWriter out = new
PrintWriter(byteStream, true);
out.print(postData);
out.flush(); // Flush since above used print, not println
out.close();
// POST requests are required to have Content-Length
String lengthString =
String.valueOf(byteStream.size());
connection.setRequestProperty
("Content-Length", lengthString);
byteStream.writeTo(connection.getOutputStream());
responseCode = connection.getResponseCode(); // 200, 404, etc
responseMsg = connection.getResponseMessage();
int i= connection.getContentLength();
BufferedReader in =
new
BufferedReader(new
InputStreamReader (connection.getInputStream())); //for reading content
String line;
String linefeed = "\n";
//.setText("");
while((line = in.readLine()) != null) {
System.out.println(line);
resultsArea.append(line);
resultsArea.append(linefeed);
}
}catch (
MalformedURLException ex){
ex.printStackTrace();
//error("Bad URL");
}catch (
UnknownServiceException ex){
ex.printStackTrace();
//error("UnknownServiceException occurred.");
}catch (
IOException ex){
ex.printStackTrace();
//error("IOException occurred.");
}
System.out.println("result is:"+resultsArea.toString()) ;
HashMap sendMap = new
HashMap();
sendMap.put("responseCode",(String.valueOf(responseCode)));
sendMap.put("responseMessage",responseMsg);
sendMap.put("Data",resultsArea.toString());
return sendMap; //235.12.1
}
Question is If I send correct data where that CGI system is able to complete request successfuly then I am getting Response Code "200" and then I am able to read content which is basically STATUS = OK.
If Request goes OK to CGI System and its doing processing but its not able to successfully do the task so CGI System generated 400 Response Code and sending HTTP Response. When I get 400 as Response code I am not able to read Content using connection.getInputStream()..I am getting
IOException on that line saying Bad Request. Is there a way to read Content even I get Response Code 400? Because in content I will get exact error related message..I need to show it on GUI. Need help on this..urgent please..
Thanks,
Radhika