Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to capture a response from a Server?

 
Vanchi Nathan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am invoking a doGet() within it I am invoking a response.sendRedirect() method. A urlString to a server is used in the sendRedirect, which responds a, "OK: sdf3sdr3423efd33esdf3", output to the browser.

Is there a way to reuse this response values in my subsequent requests?

thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Vanchi Nathan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ben,

I did the following, but still have an connection error "java.net.SocketException: Network is unreachable: connect". I'm not sure why this error, any ideas?

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

String urlStr = new String ("http://api.clickatell.com/http/auth?api_id="
+ api_id + "&user="
+ username + "&password=" + password);

HttpServletResponseWrapper hsrw = new HttpServletResponseWrapper(res);

urlStr = res.encodeRedirectURL(urlStr);

BufferedReader in = null;
BufferedWriter out = null;

try {
URI uri = new URI(urlStr);
URL url = uri.toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");

String s = conn.getResponseMessage();
System.out.println(s);

} catch (URISyntaxException e) {
e.printStackTrace();
} catch (MalformedURLException e1){
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e3) {}
}

}
===============================
The exception occurred in String s = conn.getResponseMessage();line.

----------------
thanks in advance.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vanchi,

I'm not sure what's wrong with your code and won't have much time to look at it. Here is a code snippet from one of my programs that uses URLConnection to read text from a webserver.

Hope it helps.

-Ben



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic