如何在 Java 中将 json 作为查询参数传递给 Rest Post Web 服务
例如:
https://testvpa.net/WebService/ViALoggingRestWS/ViALoggingService.svc/StartCall ?parameter={"machineName":"KK-IVR01","appName":"KKApp","startTime":"2018-02-06T21:38:32","portID":"01","ani":"9189280000","dnis":"8559281111","ctiCallID":"01"}
我正在尝试这样的事情:
...
try{
JSONObject obj = new JSONObject();
obj.put("machineName",machineName);
obj.put("appName", appName);
obj.put("startTime", formattedCurrentDate);
obj.put("portID",portID);
obj.put("ani",ani);
obj.put("dnis", dnis);
obj.put("ctiCallID", ctiCallID);
String strobj = obj.toString();
String uri = wsUri+"/StartCall?";
HttpClient client = new HttpClient();
client.getParams().setConnectionManagerTimeout(1300);
client.getParams().setSoTimeout(13000);
PostMethod method = new PostMethod(uri);
method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
method.setQueryString("parameter="+strobj );
int statusCode = client.executeMethod(method);
byte[] responseBody = method.getResponseBody();
output = new String(responseBody);
}
...
但我在运行时收到“无效的 URI”。它似乎不喜欢查询参数是 json 字符串。我在某处读到过有关对 json 字符串进行编码的内容...我是否需要对 json 字符串进行编码?
最佳答案
如果您使用 POST 请求,则应在请求正文中传递 json 对象,而不是在查询参数中传递。
关于java - 如何在 java 中将 json 作为 Rest Post Web 服务的查询参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49035953/