我正在尝试使用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);
}
}