Java 使用HttpURLConnection提交json POST请求
我试图在Scala中使用HttpURLConnection提交json post请求。我根据两个教程编写了以下内容:Java 使用HttpURLConnection提交json POST请求,java,json,scala,curl,httprequest,Java,Json,Scala,Curl,Httprequest,我试图在Scala中使用HttpURLConnection提交json post请求。我根据两个教程编写了以下内容: def sendPost(url: String, jsonHash: String) { val conn: HttpURLConnection = new URL(url).openConnection().asInstanceOf[HttpURLConnection] conn.setRequestMethod("POST") conn.setRequestPr
def sendPost(url: String, jsonHash: String) {
val conn: HttpURLConnection = new URL(url).openConnection().asInstanceOf[HttpURLConnection]
conn.setRequestMethod("POST")
conn.setRequestProperty("Content-Type", "application/json")
conn.setRequestProperty("Accept", "application/json")
conn.setDoOutput(true)
conn.connect()
val wr = new DataOutputStream(conn.getOutputStream)
wr.writeBytes(jsonHash)
wr.flush()
wr.close()
val responseCode = conn.getResponseCode
println("Sent: " + jsonHash + " to " + url + " received " + responseCode)
val in = new BufferedReader(new InputStreamReader(conn.getInputStream))
var response: String = ""
while(response != null) {
response = in.readLine()
println(response)
}
in.close()
}
它的答复是:
Sent: '{"schedule":"R/2014-02-02T00:00:00Z/PT24H", "name":"Scala-Post-Test", "command":"which scalac", "epsilon":"PT15M", "owner":"myemail@thecompany.com", "async":false}' to http://localhost:4040/scheduler/iso8601 received 500
java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:4040/scheduler/iso8601
源于
val in = new BufferedReader(new InputStreamReader(conn.getInputStream))
但如果我将其重建为curl
请求,它就可以正常工作:
curl -X POST -H 'Content-Type: application/json' -d '{"schedule":"R/2014-02-02T00:00:00Z/PT24H", "name":"Scala-Post-Test", "command":"which scalac", "epsilon":"PT15M", "owner":"myemail@thecompany.com", "async":false}' http://localhost:4040/scheduler/iso8601
requirement failed: Vertex already exists in graph Scala-Post-Test
(这正是我所期望的)
有什么见识吗?我现在正尝试嗅探数据包,以确定有什么不同
(注意:我以前放弃过)问题是:
Sent: '{"schedule":"R/2014-02-02T00:00:00Z/PT24H", "name":"Scala-Post-Test", "command":"which scalac", "epsilon":"PT15M", "owner":"myemail@thecompany.com", "async":false}' to http://localhost:4040/scheduler/iso8601 received 500
您会注意到JSON被单引号包围。这使它无效
另外值得注意的是,当这段代码运行时,您正在使用DataOutputStream.writeBytes()
来输出数据。如果字符串中不包含单字节字符,则会出现问题;它从每个字符中去掉高8位(Java使用2字节字符来保存UTF-16码点)
最好使用更适合String
输出的东西。使用与输入相同的技术,例如:
BufferedWriter out =
new BufferedWriter(new OutputStreamWriter(conn.getOutputStream));
out.write(jsonString);
out.close();
问题在于:
Sent: '{"schedule":"R/2014-02-02T00:00:00Z/PT24H", "name":"Scala-Post-Test", "command":"which scalac", "epsilon":"PT15M", "owner":"myemail@thecompany.com", "async":false}' to http://localhost:4040/scheduler/iso8601 received 500
您会注意到JSON被单引号包围。这使它无效
另外值得注意的是,当这段代码运行时,您正在使用DataOutputStream.writeBytes()
来输出数据。如果字符串中不包含单字节字符,则会出现问题;它从每个字符中去掉高8位(Java使用2字节字符来保存UTF-16码点)
最好使用更适合String
输出的东西。使用与输入相同的技术,例如:
BufferedWriter out =
new BufferedWriter(new OutputStreamWriter(conn.getOutputStream));
out.write(jsonString);
out.close();
使用PrintWriter
(而不是DataOutputStream
)。^^暂时忽略它。虽然在这里使用DataOutputStream
并不常见,但在这种情况下没有区别。当您输出jsonHash
字符串时,它将显示由单引号包围的JSON;这可能就是问题所在。啊哈!你是对的,单引号导致了500--呃,也许nc-l现在看起来是一样的,但是400个错误的请求。搜索继续。@Austin用-v
运行curl。。。那里的返回码是什么?啊哈!它还返回400!400毕竟是正确的结果。谢谢使用PrintWriter
(而不是DataOutputStream
)。^^暂时忽略它。虽然在这里使用DataOutputStream
并不常见,但在这种情况下没有区别。当您输出jsonHash
字符串时,它将显示由单引号包围的JSON;这可能就是问题所在。啊哈!你是对的,单引号导致了500--呃,也许nc-l现在看起来是一样的,但是400个错误的请求。搜索继续。@Austin用-v
运行curl。。。那里的返回码是什么?啊哈!它还返回400!400毕竟是正确的结果。谢谢