我正在使用AmazonHttpClient& AmazonWebServiceRequest从Android应用程序向API网关发出http请求。 requestBody
是一个JSON字符串,而DefaultRequest似乎只接受InputStream
作为内容。
final AmazonWebServiceRequest awsRequest = new AmazonWebServiceRequest() {};
final Request request = new DefaultRequest(awsRequest, UtilConstants.API_GATEWAY_SERVICE_NAME);
request.setEndpoint(uri);
request.setHttpMethod(requestType);
request.addHeader(HttpHeader.CONTENT_TYPE, "application/json");
final InputStream stream = new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8));
request.setContent(stream);
当我提出请求时,我得到了异常
Caused by: com.amazonaws.AmazonClientException: Unable to execute HTTP request: expected 0 bytes but received 38
答案 0 :(得分:0)
添加HttpHeader.CONTENT_LENGTH
标题解决了这个问题。