向 Web 服务发出 HTTP 获取请求时出现 405 错误

Getting 405 error when making a HTTP get request to a webservice

我正在从基本 java 程序向 URL 发出 HTTP GET 请求,而 URL 恰好是 "http://localhost:9992/users/john.doe@example.com":

public class Tester {
    public static void main(String[] args) {
        HttpRequester.doesUserExist("john.doe@example.com");
    }
}

HTTPRequester 的实现是这样的:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpRequester {
    private static HttpURLConnection connection = null;

    public static boolean doesUserExist(final String email) {
    final String targetUrl = Constants.URL_USER_SRVC +  email;
    System.out.println(targetUrl);
    try {
        URL url = new URL(targetUrl);
        connection = (HttpURLConnection) url.openConnection();
        System.out.println(connection.getRequestMethod());
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setUseCaches(false);
        connection.setDoOutput(true);

        DataOutputStream outputStream = new DataOutputStream(
                connection.getOutputStream());
        outputStream.writeBytes(email);
        outputStream.close();

            InputStream inputStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer response = new StringBuffer();
            String line;
            while((line = reader.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            reader.close();
            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}

Grizzly 服务器中嵌入的 Web 服务和 API:

@Path("/users")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class UserResource {

    @GET
    public List<User> getUsers() {
        return UserService.getUsers();
    }

    @GET
    @Path("/{userEmail}")
    public User getUser(@PathParam("userEmail") String userEmail) {
        return UserService.getUser(userEmail);
    }
}

请注意,webservice 和 java 程序是两个独立的项目。当我执行主要方法时,我在控制台中得到这个错误输出:

java.io.IOException: Server returned HTTP response code: 405 for URL: http://localhost:9992/users/john.doe@example.com
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
at com.axa.visualizr.auth.utils.HttpRequester.doesUserExist(HttpRequester.java:30)
at com.axa.visualizr.auth.core.Tester.main(Tester.java:8)

我觉得奇怪的是,即使我导入了 java.net.HttpURLConnection;,错误也会输出这个 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)。我尝试打开该行的声明,但 Eclipse 声明它不是 class.

中的有效行号

不知道为什么我有setRequestMethod("GET")的时候说方法不被允许。也许是因为 java 程序不在任何类型的服务器上 运行? Tester.java 仅用于测试目的,最终我会将 HTTPRequester 移动到另一个网络服务,并从网络服务 A 调用网络服务 B。

试试下面的代码:

final String targetUrl = Constants.URL_USER_SRVC +  URLEncoder.encode(email, "UTF-8");

而不是:final String targetUrl = Constants.URL_USER_SRVC + email;

由于您在请求体中传递了一些内容,java 将其解释为 POST 请求。请从您的代码中删除以下行并尝试:

DataOutputStream outputStream = new DataOutputStream(
                connection.getOutputStream());
        outputStream.writeBytes(email);
        outputStream.close();