java.io.IOException:服务器返回HTTP响应代码:405表示URL [英] java.io.IOException: Server returned HTTP response code: 405 for URL

查看:4956
本文介绍了java.io.IOException:服务器返回HTTP响应代码:405表示URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在线servlet,我正在尝试联系以进行一些基本测试。这是servlet代码:

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());
               }

     }
}

但是我继续得到相同的405错误。我做错了什么?

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

推荐答案

你看到的是 HttpServlet doPost()的默认实现,因为你覆盖它在 index servlet。

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);
    }
}

立即发送405响应。

which immediately sends a 405 response.

这是因为你打电话

conn.getOutputStream()

这使得 URLConnection 认为你发送的是 POST 默认情况下,不是您期望的 GET 。你甚至没有使用 OutputStream 所以为什么要打开它,然后 flush()它再也不用了它?

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();

这篇关于java.io.IOException:服务器返回HTTP响应代码:405表示URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆