Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How to capture a response from a Server?

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Vanchi Nathan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



reply
    Bookmark Topic Watch Topic
  • New Topic