JSP make http get request and get json response

2019-01-26 17:57发布

问题:

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

回答1:

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);
%>