如何向JavaSpring服务器发出http请求?

pgvzfuti  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(258)

我已经部署了一个spring服务器,我想发出一个请求,但是我总是得到相同的错误:服务器返回了http响应代码:400(错误的请求)。这是客户端代码(只是我在互联网上找到的一些复制代码):

public static void login(String username , String password) throws IOException {
        URL url = new URL("https://radiant-bayou-97811.herokuapp.com/api/user/login");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");
        Map<String, String> params = new HashMap<>();
        params.put("name",username);
        params.put("password",password);

        StringBuilder requestData = new StringBuilder();

        for (Map.Entry<String, String> param : params.entrySet()) {
            if (requestData.length() != 0) {
                requestData.append('&');
            }
            // Encode the parameter based on the parameter map we've defined
            // and append the values from the map to form a single parameter
            requestData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            requestData.append('=');
            requestData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }

        // Convert the requestData into bytes
        byte[] requestDataBytes = requestData.toString().getBytes("UTF-8");

        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Content-Length", String.valueOf(requestDataBytes.length));

        // To store our response
        StringBuilder content;

        // Get the input stream of the connection
        try (BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            String line;
            content = new StringBuilder();
            while ((line = input.readLine()) != null) {
                // Append each line of the response and separate them
                content.append(line);
                content.append(System.lineSeparator());
            }
        } finally {
            connection.disconnect();
        }

// Output the content to the console
        System.out.println(content.toString());
    }

基本上我只调用一些随机值的函数。如果我使用具有相同参数和相同url的postman,这似乎可以很好地工作,但是我无法在java代码中使用它。我怎样才能解决这个问题?这是服务器代码(如果有帮助):

@RequestMapping("api/user")
@RestController
public class UserController {
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("login")
    @GetMapping
    public String login(@RequestParam User user){
        return userService.login(user);
    }
}

用户类:

public class User {
    private final String name;
    private final String password;

    public User(@JsonProperty("name") String name,
                @JsonProperty("password") String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }
}

以及用户服务:

@Component
public class UserService {

    public String login(User user){
        return "Succesfully logged in!";
    }
}

我还尝试发送一个json文件,但得到了相同的结果。我也尝试在本地托管,但没有任何改变。p、 如果我发送一个没有参数的请求,这个就可以了。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题