我是Web应用程序和Servlet的新手,我有以下问题:
每当我在servlet中打印一些东西并通过webbrowser调用它时,它就会返回一个包含该文本的新页面.有没有办法使用Ajax在当前页面中打印文本?
如何使用HttpURLConnection发布JSON数据?我在尝试这个:
HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();
StringReader reader = new StringReader("{'value': 7.5}");
OutputStream os = httpcon.getOutputStream();
char[] buffer = new char[4096];
int bytes_read;
while((bytes_read = reader.read(buffer)) != -1) {
os.write(buffer, 0, bytes_read);// I am getting compilation error here
}
os.close();
Run Code Online (Sandbox Code Playgroud)
我在第14行遇到编译错误.
cURL请求是:
curl -H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "{'value': 7.5}" \
"a URL"
Run Code Online (Sandbox Code Playgroud)
这是处理cURL请求的方法吗?任何信息对我都非常有帮助.
谢谢.