copying the request header from request object to urlConnection object

Posted by Bunny Rabbit on Stack Overflow See other posts from Stack Overflow or by Bunny Rabbit
Published on 2010-04-01T10:24:10Z Indexed on 2010/04/01 10:33 UTC
Read the original article Hit count: 358

Filed under:
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
     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();

}

i wrote the above code in a servlet to post form so some alternate locations than this servlet,but its not working.is it okay to use connection.setRequestProperty to set the header fields to what they are in the incoming request to servlet.

© Stack Overflow or respective owner

Related posts about java

Related posts about servlets