JSP make http get request and get json response

2019-01-26 18:02发布

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条回答
时光不老,我们不散
2楼-- · 2019-01-26 18:47

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);
%>
查看更多
登录 后发表回答