有 Tomcat 8(设置在端口 9001 上)和一个带有上下文路径/的 Servlet,项目名称是“ROOT”,所以每个请求都像:http://myserver:9001/smthng/ ... 必须由 Servlet 接收。
客户端(最终用户)将他们的浏览器代理设置为 http://myserver:9001 ,所以我的 Servlet 从客户端获取请求并使用另一个代理发送新请求并获取响应,然后将响应发送给客户端。
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("##### header(host) ##### " + request.getHeader("host"));
System.out.println("##### getRemoteAddr ##### " + request.getRemoteAddr());
System.out.println("##### request.getScheme() ##### " + request.getScheme());
String proxyAdress = "Proxy.haccettepe.edu.tr";
String proxyPort = "8080";
System.setProperty("http.proxyHost", proxyAdress);
System.setProperty("http.proxyPort", proxyPort);
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$");
return (new PasswordAuthentication("yildirims",
"9891".toCharArray()));
}
};
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%% " + authenticator.toString());
Authenticator.setDefault(authenticator);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("Proxy.haccettepe.edu.tr", 8080));
///////---->HttpURLConnection with proxy
URL url = new URL((request.getScheme() + "://" + request.getHeader("host") + request.getRequestURI() + "?" + request.getQueryString()).replace("?null", ""));
System.out.println("URL ============= " + url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setRequestMethod(request.getMethod());
System.out.println(">>>>>>>>>>>>>>>>>>>" + conn.usingProxy());
conn.connect();
InputStream is2 = conn.getInputStream();
System.out.println("content type=>>>>>>>"+ conn.getContentType());
System.out.println("content getContentEncoding=>>>>>>>"+ conn.getContentEncoding());
System.out.println("content getHeaderFieldKey=>>>>>>>"+ conn.getHeaderFieldKey(0));
System.out.println("content getRequestMethod=>>>>>>>"+ conn.getRequestMethod());
HttpServletResponse rs=new HttpServletResponseWrapper(response);
OutputStream oos = response.getOutputStream();
response.setHeader("Content-type", conn.getContentType());
response.setContentType(conn.getContentType());
byte[] buf = new byte[102400];
int c = 0;
while ((c = is2.read(buf, 0, buf.length)) > 0) {
oos.write(buf, 0, c);
if (url.toString().endsWith(".pdf")) {
oos.flush();
}
}
if (url.toString().endsWith(".pdf")) {
oos.close();
is2.close();
}
}
所以这段代码适用于一些简单的页面并下载一些简单的 .pdf 文件,但不适用于像这样的某些 .pdf 文件:
http://link.springer.com/content/pdf/10.1007%2Fs00128-015-1658-6.pdf
下面的代码适用于许多 http 网页,
最佳答案
一些 url 有/所以配置 tomcat 不拒绝它们
一些请求已发布,需要一些 cokies 才能下载它们。
关于java Servlet代理不下载某些pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39930498/