protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
URL url = new URL("http://localhost:8080/testy/Out");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
PrintWriter out = response.getWriter();
for(Enumeration e = request.getHeaderNames(); e.hasMoreElements();) {
Object o = e.nextElement();
String value = request.getHeader(o.toString());
out.println(o + "--is--" + value + "<br>");
connection.setRequestProperty((String) o, value);
}
connection.connect();
}
我在一个 servlet 中编写了上面的代码来发布表单,所以一些替代位置比这个 servlet,但它不起作用。可以使用 connection.setRequestProperty 将标头字段设置为它们在传入的 servlet 请求中的内容。