这是我的简单程序(try catch 块内的代码),它将负责调用正在运行的 web 服务。基本上,这负责将应用程序的事件记录到 webService:
更新的问题代码
package com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class NetClientPost {
HttpURLConnection conn = null;
NetClientPost() throws Exception {
URL url = new URL("http://localhost:8080/RestTest/custom/log_service");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
}
public static void main(String[] args) {
NetClientPost po;
try {
po = new NetClientPost();
po.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void execute() {
try {
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"qty\":100,\"name\":\"eee 4\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}