简体   繁体   中英

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" :

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

The implementation of the HTTPRequester is this:

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:

@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. When I execute the main method I get this error output in the console:

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; . I have tried opening the declaration on that line but Eclipse states that it is not an valid line number in that class.

I don't know why it's saying that the method is not allowed when I have setRequestMethod("GET") . Maybe it might be because the java program is not running on any kind of server? 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.

Since you are passing something in the requestbody, java is interpreting it as a POST request. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM