aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Problem in reading Http Response Content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Problem in reading Http Response Content" Watch "Problem in reading Http Response Content" New topic
Author

Problem in reading Http Response Content

Radhika Reddy
Ranch Hand

Joined: Oct 31, 2002
Posts: 30
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
Radhika Reddy
Ranch Hand

Joined: Oct 31, 2002
Posts: 30
I am using URLConnection to send POST request to CGI program and it is returning 403 Response code with error message. Whenever my code receives 403 it is throwing error IOException on line
BufferedReader in =
new BufferedReader(new InputStreamReader
(connection.getInputStream())); //for reading content

if Response code is 200 then I am able to read body content whereas 403 I cannot using URLConnection. Even though the response code is 400 or 403 etc I need to read body content and display that message on GUI. I don't know how to do it.Its very urgent..need help..please..

Thanks,
Radhika
Rakesh Jain
Greenhorn

Joined: Aug 18, 2004
Posts: 2
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



Even I am facing the similar problem . Radhika did you get an resolution for this, If then can you please post that.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Well, if you don't get a good return code there won't be any body. Is the message you seek not in your responseMessage field? This message is in the first header in the response, not in the body.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in reading Http Response Content