JSP make http get request and get json response

17,657

Here is the sample code. You'll have response in recvbuff.

<%@page import="java.io.*" %>
<%@page import="java.net.*" %>

<%
   String recv;
   String recvbuff;
   URL jsonpage = new URL("http://www.yoursite.com/jsonresponse");
   URLConnection urlcon = jsonpage.openConnection();
   BufferedReader buffread = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));

   while ((recv = buffread.readLine()) != null)
    recvbuff += recv;
   buffread.close();

   System.out.println(recvbuff);
%>
Share:
17,657

Related videos on Youtube

user1375026
Author by

user1375026

Updated on September 16, 2022

Comments

  • user1375026
    user1375026 over 1 year

    I would like to make a http get request which gets a json response. In that session response I would like to store a value into my session. How is this achievable?

    Thanks

  • Alexandre Lavoie
    Alexandre Lavoie over 11 years
    Simply use the button "{}" it will indent all the selected zone and will appear as code.
  • Vahid Farahmand
    Vahid Farahmand over 11 years
    did that, but as it contains <% tags, (I think) my code disappeared in previw. Maybe I was doing something wrong...
  • user1375026
    user1375026 over 11 years
    And what is dest here? Is it meant to be jsonpage? Thanks for the help
  • Shreyas
    Shreyas almost 8 years
    Shouldn't the URLConnection part and the BufferedReader part be surrounded in the try-catch block ?