将 Java 中的简单 JSON 对象发送到 Servlet
Send a simple JSON object in Java to a Servlet
我正在尝试从 java 客户端向 servlet 发送一个 JSONObject (org.json),但是在我的服务器端我得到 'null' for HttpServletRequest.getParameter ("Command") 或任何参数。
在我的客户端:
JSONObject json = new JSONObject();
try{
json.put("Command","spost");
json.put("Name","pc1");
json.put("Pwd","pc1");
sendRequest(json);
} catch(JSONException jsone){
}
URL url;
HttpURLConnection connection = null;
ObjectOutputStream out;
try {
url = new URL("http://myURL.com/myservlet"); //Creating the URL.
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
//connection.setRequestProperty("Accept", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
System.out.println(json.toString());
osw.write(json.toString());
osw.flush();
osw.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Ok response");
} else {
System.out.println("Bad response");
}
} catch (Exception ex) {
ex.printStackTrace();
}
然后我在打印 json.toString():
时得到这样的东西
{"Name":"pc1","Command":"SignalPost","Pwd":"pc1"}
...这对我来说很正常。
我看到一个 "Ok response" 并且我的 servlet 检测到 httprequest,但似乎对 json 对象的理解有问题
我的 servlet 对于我使用 AJAX 创建的另一个客户端工作正常,所以我猜问题出在这个 java 客户端。
你能帮帮我吗?我用谷歌搜索并尝试了所有方法,但没有成功
谢谢
编辑:
最后,在服务器端,这段代码是有效的:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
StringBuilder sb = new StringBuilder();
BufferedReader br = request.getReader();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str);
System.out.println(str);
}
JSONObject jObj = new JSONObject(sb.toString());
String name = jObj.getString("Name");
String pwd = jObj.getString("Pwd");
String command = jObj.getString("Command");
JSONObject json = new JSONObject();
response.setContentType("application/json");
response.setHeader("Cache-Control", "nocache");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print(json.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
虽然我更愿意继续使用来自客户端的 GET 请求,这样我就不必重新制作我所有的 servlet 端
尝试添加 connection.connect()
try {
url = new URL("http://myURL.com/myservlet"); //Creating the URL.
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
//connection.setRequestProperty("Accept", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect() //New line
//Send request
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
System.out.println(json.toString());
osw.write(json.toString());
osw.flush();
osw.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Ok response");
} else {
System.out.println("Bad response");
}
您的 json 对象未作为请求参数发送,而是在请求正文中发送。
因此,在您的服务器端 servlet 中,您不必尝试从任何请求参数中恢复它,您必须从 HttpServletRequest 的 InputStream 中读取它。
阅读它,然后使用您在 servlet 方法中选择的 json 库解析它,您将获得它。
我正在尝试从 java 客户端向 servlet 发送一个 JSONObject (org.json),但是在我的服务器端我得到 'null' for HttpServletRequest.getParameter ("Command") 或任何参数。
在我的客户端:
JSONObject json = new JSONObject();
try{
json.put("Command","spost");
json.put("Name","pc1");
json.put("Pwd","pc1");
sendRequest(json);
} catch(JSONException jsone){
}
URL url;
HttpURLConnection connection = null;
ObjectOutputStream out;
try {
url = new URL("http://myURL.com/myservlet"); //Creating the URL.
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
//connection.setRequestProperty("Accept", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
System.out.println(json.toString());
osw.write(json.toString());
osw.flush();
osw.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Ok response");
} else {
System.out.println("Bad response");
}
} catch (Exception ex) {
ex.printStackTrace();
}
然后我在打印 json.toString():
时得到这样的东西{"Name":"pc1","Command":"SignalPost","Pwd":"pc1"}
...这对我来说很正常。
我看到一个 "Ok response" 并且我的 servlet 检测到 httprequest,但似乎对 json 对象的理解有问题
我的 servlet 对于我使用 AJAX 创建的另一个客户端工作正常,所以我猜问题出在这个 java 客户端。
你能帮帮我吗?我用谷歌搜索并尝试了所有方法,但没有成功
谢谢
编辑:
最后,在服务器端,这段代码是有效的:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
StringBuilder sb = new StringBuilder();
BufferedReader br = request.getReader();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str);
System.out.println(str);
}
JSONObject jObj = new JSONObject(sb.toString());
String name = jObj.getString("Name");
String pwd = jObj.getString("Pwd");
String command = jObj.getString("Command");
JSONObject json = new JSONObject();
response.setContentType("application/json");
response.setHeader("Cache-Control", "nocache");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print(json.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
虽然我更愿意继续使用来自客户端的 GET 请求,这样我就不必重新制作我所有的 servlet 端
尝试添加 connection.connect()
try {
url = new URL("http://myURL.com/myservlet"); //Creating the URL.
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
//connection.setRequestProperty("Accept", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect() //New line
//Send request
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
System.out.println(json.toString());
osw.write(json.toString());
osw.flush();
osw.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Ok response");
} else {
System.out.println("Bad response");
}
您的 json 对象未作为请求参数发送,而是在请求正文中发送。
因此,在您的服务器端 servlet 中,您不必尝试从任何请求参数中恢复它,您必须从 HttpServletRequest 的 InputStream 中读取它。
阅读它,然后使用您在 servlet 方法中选择的 json 库解析它,您将获得它。