Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a servlet online that I'm trying to contact in order to do some basic testing. This is the servlet code:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class index extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public index() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        long time1 = System.currentTimeMillis();
        long time2 = time1 + 10000;
        out.println(time1);
        long i = 400000000l;
            while (System.currentTimeMillis() < time2) {
                i++;
            }
            out.print(time2);
    }
}

Now, I'm trying to get information from the server using the following code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpRequest {
    public static void main(String args[]) {
        BufferedReader rd;
        OutputStreamWriter wr;

            try {
                URL url = new URL("http://blahblahblah/index");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
                wr = new OutputStreamWriter(conn.getOutputStream());
                wr.flush();

                conn.setConnectTimeout(50000);
                rd = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = rd.readLine()) != null) {
                    System.out.println(line);
                }
             } catch (Exception e) {
                 System.out.println(e.toString());
               }

     }
}

However I keep getting the same 405 error. What am I doing wrong?

share|improve this question

1 Answer

up vote 2 down vote accepted

What you are seeing is the HttpServlet's default implementation of doPost(), since you don't override it in your index servlet.

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
{
    String protocol = req.getProtocol();
    String msg = lStrings.getString("http.method_post_not_supported");
    if (protocol.endsWith("1.1")) {
        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
    } else {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
    }
}

which immediately sends a 405 response.

This occurs because you call

conn.getOutputStream()

which makes the URLConnection think you are sending a POST by default, not the GET you are expecting. You aren't even using the OutputStream so why open it, then flush() it and never us it again?

wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();
share|improve this answer
 
Thanks, I deleted those two lines and it worked. –  spacitron Sep 8 at 18:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.