我正在尝试使用AsyncHttpClient发出JSON请求。我尝试使用几种实现,包括setBody和addBodyPart等。结果总是一样。
它应该发送这样的字符串,将其转换为字节:
{
"template": "c=KZ;o=Template;cn=GOST_RAUTIL_USER_1Y;",
"useHSM": "true",
"altAttrs": {
"altDN": [
"CN=Test org1, SERIALNUMBER=1111111111",
"CN=Test org2, SERIALNUMBER=2222222222"
]
},
"keyName": "XkjCPFLfvJCUYktH",
"dn": "C=KZ, O=TEST, CN= XkjCPFLfvJCUYktH",
"internalHSMkey": "true"
}
但是以某种方式它将请求转换为:
.. an ip 地址.. POST标头:Content-Type:应用程序/ json授权:基本 SWdvcjpHYW1tYTIwMTk =
因此服务器收到错误的请求和响应并带有错误消息:
JSONObject文本必须在0 [字符1行1] json处以“ {”开头:
这是我的代码实现之一:
Request request = new RequestBuilder(POST)
.setUrl(apiUrl)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic " + DatatypeConverter.printBase64Binary(userPassword.getBytes()))
.setBody(jsonObject.toString().getBytes())
.build();
asyncHttpClient.executeRequest(request, new AsyncCompletionHandlerBase() {
@Override
public void onThrowable(Throwable t) {
}
@Override
public org.asynchttpclient.Response onCompleted(org.asynchttpclient.Response response) throws Exception {
System.out.println("COMPLETED");
return super.onCompleted(response);
}
});