tmn*_*d91 3 java scala get http
我知道你不应该发送带有正文的 HTTP GET 请求,但是 ceilometer web api 强迫我这样做。我正在开发一个 ceilometer scala 客户端,所以我需要一种 scala/java 方式来发出带有主体的 get 请求。到目前为止,我尝试使用 beeClient ( http://www.bigbeeconsultants.co.uk ) 和使用 httpConnection 的纯 Java,但出现 404 错误。在 curl 我可以通过这种方式实现结果:
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/
Run Code Online (Sandbox Code Playgroud)
那是我使用 java HttpURLConnection 的 Scala 代码:
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")
Run Code Online (Sandbox Code Playgroud)
我的 Java 实现和 curl 命令之间有什么区别?我看不到任何东西,我尝试检查 curl 的标题,使用 -v 参数调用它,这就是我得到的:
* 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
Run Code Online (Sandbox Code Playgroud)
然后我得到了回应。
先感谢您
小智 6
我使用 jetty-client 实现解决了这个问题,它可以让你以任何你想要的方式构建 http 请求。这是代码(它不是一成不变的,但在 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()
Run Code Online (Sandbox Code Playgroud)
看,我正在使用阻塞 API,但 jetty 还提供了 Java NIO API。
我在这里找到了一个工作简单的java解决方案,使用apache的httpclient、、httpcore和commons-logging库。
您需要创建一个类并扩展HttpEntityEnclosingRequestBase,覆盖方法名称:
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
public final static String METHOD_NAME = "GET";
@Override
public String getMethod() {
return METHOD_NAME;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你就这样使用它:
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGetWithEntity e = new HttpGetWithEntity();
e.setURI(new URI(yourURL))
e.setEntity(yourEntity);
HttpResponse response = httpclient.execute(e);
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。