Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes How to capture a response from a Server? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to capture a response from a Server?" Watch "How to capture a response from a Server?" New topic
Author

How to capture a response from a Server?

Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
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


Best regards,<br /> <br />vanchin
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Instead of redirecting make the HTTP call directly from your servlet and parse the results.

See: http://jakarta.apache.org/commons/httpclient

or

http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLConnection.html


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
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

Joined: Dec 11, 2004
Posts: 13410

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



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to capture a response from a Server?
 
Similar Threads
How to increase number of parellel ajax hits from same user!!
compareTo() and Collections.sort() Solution For Multiple Column Ordering (Ascending) question
getting the URI response page in struts actio servlet
AJAX: request not sent properly
IllegalStateException: Cannot forward. Response already committed.