简体   繁体   English

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

[英]Getting 405 error when making a HTTP get request to a webservice

I'm making the HTTP GET request from a basic java program to a URL which happens to be "http://localhost:9992/users/john.doe@example.com" : 我正在从基本的Java程序向碰巧是"http://localhost:9992/users/john.doe@example.com"的URL发出HTTP GET请求:

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

The implementation of the HTTPRequester is this: 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;
    }
}

The webservice embedded in a Grizzly server and here are the APIs: 嵌入在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);
    }
}

Please take note that the webservice and java program are two separate projects. 请注意,webservice和java程序是两个单独的项目。 When I execute the main method I get this error output in the console: 当我执行main方法时,我在控制台中得到以下错误输出:

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)

What I think is odd is that the error outputs this at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627) even though I imported java.net.HttpURLConnection; 我认为奇怪的是,即使我导入了java.net.HttpURLConnection; ,该错误也会at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)输出此错误java.net.HttpURLConnection; . I have tried opening the declaration on that line but Eclipse states that it is not an valid line number in that class. 我尝试过在该行上打开声明,但是Eclipse指出它不是该类中的有效行号。

I don't know why it's saying that the method is not allowed when I have setRequestMethod("GET") . 我不知道为什么说当我有setRequestMethod("GET")时不允许使用该方法。 Maybe it might be because the java program is not running on any kind of server? 也许是因为Java程序未在任何类型的服务器上运行? The Tester.java is only for testing purposes eventually I will move HTTPRequester to another webservice and make the call from webservice A to webservice B. Tester.java仅用于测试目的,最终我将HTTPRequester移至另一个Web服务,并从Web服务A调用到Web服务B。

Since you are passing something in the requestbody, java is interpreting it as a POST request. 由于您要在请求正文中传递某些内容,因此java会将其解释为POST请求。 Please remove below lines from your code and try: 请从代码中删除以下几行,然后尝试:

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

Try below code: 试试下面的代码:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC:HTTP 405-发出POST请求时不支持请求方法“ GET” - Spring MVC: HTTP 405 - Request method 'GET' not supported when making POST request 转发Servlet POST请求时出现HTTP错误405 - HTTP error 405 when forwarding servlet POST request 发送okhttp请求时:HTTP错误405和invalid_client - When sending okhttp request: HTTP ERROR 405 and invalid_client 405发送HTTP请求时抛出错误JAVA - 405 Error when sending HTTP request throw JAVA 获取“405 - 调用”method = DELETE时不支持请求方法&#39;GET&#39; - Getting “405 - Request method 'GET' not supported when calling” method=DELETE HTTP状态405 - 不支持请求方法“GET” - HTTP Status 405 - Request method 'GET' not supported 获取错误HTTP状态405 - 此方法不支持HTTP方法GET但不使用`get`? - getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever? HTTP状态405-此URL不支持HTTP方法GET-RequestDispatcher出现错误 - HTTP Status 405 - HTTP method GET is not supported by this URL - getting error with RequestDispatcher angular4 http得到405错误 - angular4 http get 405 error 从JSP调用servlet时出现“ HTTP状态405 –此URL不支持HTTP方法GET”错误 - “HTTP Status 405 – HTTP method GET is not supported by this URL” error when calling servlet from JSP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM