http获取带有正文的请求 [英] http get request with body

查看:32
本文介绍了http获取带有正文的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您不应该发送带有正文的 HTTP GET 请求,但是 ceilometer web api 强迫我这样做.我正在开发一个 ceilometer scala 客户端,所以我需要一种 scala/java 方式来使用主体发出 get 请求.到目前为止,我尝试使用 beeClient (http://www.bigbeeconsultants.co.uk) 和纯 Java使用 httpConnection 但我收到 404 错误.在 curl 我可以通过这种方式实现结果:

i know you shouldn't send a HTTP GET Request with a body, but ceilometer web api forces me to do so. I'm developing a ceilometer scala client, so I need a scala/java way to make a get request with a body. So far I tried with beeClient (http://www.bigbeeconsultants.co.uk) and in plain Java using httpConnection but I get a 404 error. In curl I can achieve the result in this way:

curl -X GET -H "X-Auth-Token: ..long long token here.." 
-H "Content-Type: application/json" 
-d '{"q": [{"field": "resource", "op": "eq", "value": "gdfsf"}]}'
http://137.204.57.150:8777/v2/meters/

这是我使用 java HttpURLConnection 的 Scala 代码:

That's my scala code that uses java HttpURLConnection:

import java.io._
import java.net._
val token = "myToken"
val url = new URL("http://137.204.57.150:8777/v2/meters/")
val body = "{"q": [{"field": "resource", "op": "eq", "value": "gdfsf"}]}"
val bodyLenght = body.length.toString
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
connection.setRequestMethod("GET")
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Content-Length", bodyLength)
connection.setRequestProperty("Accept", "*/*")
connection.setRequestProperty("X-Auth-Token", token)
connection.setDoInput(true)
connection.setDoOutput(true)
//SEND REQUEST
val wr = new DataOutputStream(connection.getOutputStream)
wr.write(body.getBytes)
wr.flush
wr.close
if (connection.getResponseCode == 200) 
    println("ok")
else
    println("error")

我的 Java 实现和 curl 命令有什么区别?我看不到任何东西,我尝试检查使用 -v 参数调用它的 curl 的标题,这就是我得到的:

What's the difference between my Java implementation and the curl command? I can't see any, I tried checking the header of curl calling it with the -v argument and that's what I get:

* Hostname was NOT found in DNS cache
*   Trying 137.204.57.150...
* Connected to 137.204.57.150 (137.204.57.150) port 8777 (#0)
> GET /v2/meters/ HTTP/1.1
> User-Agent: curl/7.37.1
> Host: 137.204.57.150:8777
> Accept: */*
> X-Auth-Token: ...Token....
> Content-Type: application/json
> Content-Length: 60
> 
* upload completely sent off: 60 out of 60 bytes
* HTTP 1.0, assume close after body

然后我得到了回应.

提前致谢

推荐答案

我使用 jetty-client 实现解决了这个问题,它可以让您以任何方式构建 http 请求.这是代码(它不是一成不变的,但在 Scala 中并没有那么糟糕):

I resolved the problem using jetty-client implementation, that lets build http requests in anyway you want. Here's the code (it's not immutable but it's not that bad in scala):

val httpClient = new HttpClient()
httpClient.setConnectTimeout(connectTimeout)
httpClient.setFollowRedirects(false)
httpClient.setStopTimeout(readTimeout)
httpClient.start()
val resp = httpClient.newRequest(uri).
      method(HttpMethod.GET).
      header("X-Auth-Token",s).
      send()

您看,我使用的是阻塞 API,但 jetty 还提供了 Java NIO API.

Look that i'm using the blocking API but jetty provides also a Java NIO API.

这篇关于http获取带有正文的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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