Posted By

bcmoney on 12/10/10


Tagged

http java JSP proxy


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

kubaitis


proxy.jsp


 / Published in: Java
 

URL: http://bcmoney-mobiletv.com/blog/2009/05/01/the-server-side-proxy/

Source of inspiration was the Proxy deployed as part of GeoDashViewer: http://trac2.assembla.com/alegia/browser/GeoDashViewer/WebContent/proxy.jsp?rev=341

As well as the idea from: http://vsingleton.blogspot.com/2006/12/simple-jsp-soap-proxy.html

After posting, saw something very similar blogged about here: https://www.codemagi.com/blog/post/190

And here: http://rrao.host.adobe.com/kannan/JSPProxyScript.jsp.txt

  1. <%@page session="false"%>
  2. <%@page import="java.net.*,java.io.*" %>
  3. <%
  4. try {
  5. String reqUrl = request.getQueryString(); //OR: request.getParameter("url");
  6.  
  7. URL url = new URL(reqUrl);
  8. HttpURLConnection con = (HttpURLConnection)url.openConnection();
  9. con.setDoOutput(true);
  10. con.setRequestMethod(request.getMethod());
  11. int clength = request.getContentLength();
  12. if(clength > 0) {
  13. con.setDoInput(true);
  14. byte[] idata = new byte[clength];
  15. request.getInputStream().read(idata, 0, clength);
  16. con.getOutputStream().write(idata, 0, clength);
  17. }
  18. response.setContentType(con.getContentType());
  19.  
  20. BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
  21. String line;
  22. while ((line = rd.readLine()) != null) {
  23. out.println(line);
  24. }
  25. rd.close();
  26.  
  27. } catch(Exception e) {
  28. response.setStatus(500);
  29. }
  30. %>

Report this snippet  

You need to login to post a comment.