使用HttpURLConnection提交一个json POST请求 [英] Using HttpURLConnection to submit a json POST request

查看:1419
本文介绍了使用HttpURLConnection提交一个json POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Scala中使用HttpURLConnection提交一个json post请求。我跟着两个教程,并产生这个:

I am trying to submit a json post request using HttpURLConnection in Scala. I followed along to two tutorials and produced this:

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 请求,它工作正常:

but if I rebuild it as a curl request, it works fine:

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

(这是我的期望)

任何洞察到什么是错的?我现在尝试嗅闻包,以确定是什么不同。

Any insight to what is wrong? I'm trying to sniff the packets now to determine what is different.

(注意:我以前放弃了 sys.process._

(Note: I had previously given up on sys.process._)

推荐答案

问题在这里:

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被单引号包围。

You'll note your JSON is surrounded by single quotes. This makes it invalid.

还值得注意的是,虽然此代码有效,但您使用的是 DataOutputStream.writeBytes ()以输出您的数据。这将是有问题的,如果你的字符串包括除了单字节字符;它剥离每个 char (Java使用2字节字符来保存UTF-16代码点)的高8位。

Also worth noting is that while this code works, you are using a DataOutputStream.writeBytes() to output your data. This would be problematic if your string including anything but single-byte characters; it strips the high 8 bits off each char (Java uses 2-byte chars to hold UTF-16 codepoints).

最好使用更适合 String 输出的东西。您用于输入的相同技术,例如:

It's better to use something more suited for String output. The same technique you use for input, for example:

BufferedWriter out = 
    new BufferedWriter(new OutputStreamWriter(conn.getOutputStream));
out.write(jsonString);
out.close();

这篇关于使用HttpURLConnection提交一个json POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆