【问题标题】:Need help on POST request using com.google.api.client.http.HttpRequest在使用 com.google.api.client.http.HttpRequest 的 POST 请求上需要帮助
【发布时间】:2020-11-21 16:58:41
【问题描述】:

我正在尝试使用com.google.api.client.http.HttpRequest 发送帖子请求,请求正文中包含数据,但收到错误com.google.api.client.http.HttpResponseException: 400 Bad Request 我需要发送请求"application/x-www-form-urlencoded" 这是我的示例:

  public static void sendMessage(String url1, String params){
       
        try {
            String urlParameters  = "{\"name\":\"myname\",\"age\":\"20\"}" ;
            byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
            HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
            GenericUrl url = new GenericUrl(url1);
            HttpContent content = new ByteArrayContent("application/x-www-form-urlencoded", postData);
            HttpRequest request = requestFactory.buildPostRequest(url, content);
            com.google.api.client.http.HttpResponse response = request.execute();
            System.out.println("Sent parameters: " + urlParameters + " - Received response: " + response.getStatusMessage());
            System.out.println("Response content: " + CharStreams.toString(new InputStreamReader(response.getContent())));
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }

【问题讨论】:

    标签: java google-api-java-client google-url-shortener google-http-client


    【解决方案1】:

    我认为您的问题是没有使用这个特定的类发送请求。但是编码参数本身。这使服务器很难解析您的请求,并作为回报给您 400 响应。

    您的版本似乎模拟了 JSON,但这不是您在 HTTP 中编码参数的方式。 正确的方法如下:

    name=myname&age=20
    

    另外,请记住,您需要对要添加到参数的所有数据进行 url 编码。否则服务器不会理解你的请求,你会再次遇到同样的问题。

    这里:https://www.baeldung.com/java-url-encoding-decoding#encode-the-url,你有一些很好的例子来说明如何使用 Java 进行 URL 编码。

    编辑:添加示例

    以下代码有效:

    String urlParameters  = "name=Cezary&age=99" ;
    byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
    HttpTransport transport = new NetHttpTransport();
    HttpRequestFactory requestFactory = transport.createRequestFactory();
    GenericUrl url = new GenericUrl("http://localhost:8080/greeting");
    HttpContent content = new ByteArrayContent("application/x-www-form-urlencoded", postData);
    HttpRequest request = requestFactory.buildPostRequest(url, content);
    com.google.api.client.http.HttpResponse response = request.execute();
    

    您可以使用UrlEncodedContent,而不是使用ByteArrayContent 并自己编码参数,它会为您处理url 编码。

    Map<String, Object> params = new HashMap<>();
    params.put("name", "Cezary");
    params.put("age", 99);
    HttpContent content = new UrlEncodedContent(params);
    

    上面提到的代码在接受 HTTP POST 的服务器上进行了测试,该服务器在正文中带有 url 编码参数。如果它仍然不适合你。您应该使用 curl/postman 或其他实用程序来验证您的服务器是否使用该实用程序。如果不是,则完全正常,它会返回 400 给您。

    【讨论】:

    • 感谢@Cezary Butler 的回复,我试过但它不起作用.. 寻找一些例子 google httpclient 如何使用"application/x-www-form-urlencoded" 发送发布请求,以便我可以尝试比较我的代码我是什么不见了。
    • @user1591156 我已经编辑了答案,请再次检查。 URL 编码参数中有一个小错误(= b in name)。在我的服务器上,这个错误可以正常工作,但我不知道你在调用什么后端。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    相关资源
    最近更新 更多