0

I have a jsp page running in a jboss 4.2.2 server. In the jsp im trying to print the headers of a http post request using methods like request.getMethod() and request.getHeaderNames(). Im sure that the request is a http post because im generating it from another program but what the jsp print is a http get request instead, the content-length printed is -1 and the headers printed doesnt match the headers im sending.

The code of the application that generate the request is, more or less, like this:

    URL url = new URL(the_url);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-type", "application/timestamp-query");
    con.setRequestProperty("Content-length", String.valueOf(data.length));
    out = con.getOutputStream();
    out.write(data);
    out.flush();

The code of the application where im trying to print the request header is:

    <%@page import="java.io.InputStream"%>
    <%@page import="java.util.Date"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="java.util.Enumeration"
    import="java.io.File"
    import="java.io.FileOutputStream"
    import="java.util.Date"%><%
    File logfile = new File("/home/tsa/logfile");
    FileOutputStream log_os = new FileOutputStream(logfile);
    Enumeration header_names = request.getHeaderNames();
    log_os.write(("cont-len: "+request.getContentLength()+"\r\n").getBytes());
    log_os.write(("method: "+request.getMethod()+"\r\n").getBytes());
    while (header_names.hasMoreElements()) {
        String name = (String)header_names.nextElement();
        log_os.write((name+": "+request.getHeader(name)+"\r\n").getBytes());
    }
    log_os.close();%>

HTTP data I should be receiving:

    POST / HTTP/1.1
    Content-type: application/timestamp-query
    User-Agent: Java/1.7.0_05
    Host: localhost:8080
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive
    Content-Length: 51

    MDECAQEwITAJBgUrDgMCGgUABBSg8UkKINAhHJl7RLw1fhly3quK4wYGBACPZwEBAQH/

HTTP data I'm receiving:

    User-Agent: Java/1.7.0_05
    Host: 192.168.56.101:8080
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive

    The method reported is GET and the Content-Length: -1

Thank you for your help.

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • Have you tried debugging it with another site? I use http://requestb.in/ for that. It will display the received data. Just change the url temporarily to point to that site. If that fails, try inspecting your Request with [Fiddler](http://www.fiddler2.com/). – mercutio Aug 02 '12 at 10:51
  • Try setting the `Content-type` to `application/x-www-form-urlencoded` – Rosdi Kasim Aug 02 '12 at 11:14
  • Or you could use Apache HttpPost, refer here: http://www.vogella.com/articles/ApacheHttpClient/article.html#example_post – Rosdi Kasim Aug 02 '12 at 11:29
  • It seems like requestb.in doesnt work properly at the moment and fiddler is showing the info of my web browser traffic only not the info of the application that generates the http request. But i have tested it receiving the http request via an inputstream in a standalone application and parsing the info and all is ok. The problem seems to reside in the way the application server receives the request. – user1570951 Aug 02 '12 at 11:35
  • I need to use that Content-type because is the standard. – user1570951 Aug 02 '12 at 11:37

1 Answers1

-1

I had the same issue. I updated to JDK 1.7.0_u21 and the problem went away.

Steven
  • 3,844
  • 3
  • 32
  • 53