Proxy.jsp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <%@page session="false"%>
  2. <%@page import="java.net.*,java.io.*" %>
  3. <%@ page pageEncoding="UTF-8" isELIgnored="false" %>
  4. <%
  5. /**
  6. *为实现客户端跨域请求而采用jsp制作的代理转发器
  7. *使用方式:
  8. *将该文件放在iserver webapps下面你的工程目录下,请求格式:http://localhost:8090/yourRoot/Proxy.jsp?url=yoururl,注意url参数中特殊字符需要转码。
  9. *支持post,get请求
  10. */
  11. try {
  12. String reqUrl = request.getParameter("url");
  13. URL url = new URL(reqUrl);
  14. HttpURLConnection con = (HttpURLConnection)url.openConnection();
  15. con.setRequestProperty("content-type", "application/xml; charset=UTF-8");
  16. con.setDoOutput(true);
  17. con.setRequestMethod(request.getMethod());
  18. //con.setCharacterEncoding("UTF-8");
  19. int clength = request.getContentLength();
  20. if(clength > 0) {
  21. con.setDoInput(true);
  22. byte[] idata = new byte[clength];
  23. //String s = String.ValueOf(clength);
  24. //System.out.println(clength);
  25. BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
  26. String line1 = null;
  27. StringBuilder sb = new StringBuilder();
  28. while((line1 = br.readLine())!=null){
  29. sb.append(line1);
  30. }
  31. //request.getInputStream().read(idata, 0, clength);
  32. //String test=String.valueOf(idata);
  33. //System.out.println(sb);
  34. //OutputStreamWriter out=new OutputStreamWriter(con.getOutputStream());
  35. //out.write(new String(sb.toString().getBytes("ISO-8859-1")));
  36. byte[] paradata = sb.toString().getBytes();
  37. con.getOutputStream().write(paradata);
  38. }
  39. response.setContentType(con.getContentType());
  40. BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
  41. String line;
  42. StringBuilder sb1 = new StringBuilder();
  43. while ((line = rd.readLine()) != null) {
  44. out.println(line);
  45. sb1.append(line);
  46. }
  47. rd.close();
  48. } catch(Exception e) {
  49. System.out.println(0);
  50. System.out.println(e);
  51. response.setStatus(500);
  52. }
  53. %>