【问题标题】:Vertx POST does not work -> error 404Vertx POST 不起作用 -> 错误 404
【发布时间】:2016-10-04 08:12:15
【问题描述】:

我不知道为什么,但是我用 Vertx 做的帖子根本不起作用。错误一直是 404。
我在纯 Java 中使用的相同链接和正文,我得到了服务器的响应。我做错了什么?

HttpClient client = vertx.createHttpClient();

HttpClientRequest request = 
client.post("https://login.windows.net/common/oauth2/token").handler(res->{
                System.out.println(res.statusCode());
            }).putHeader(HttpHeaders.CONTENT_LENGTH,String.valueOf(buffer.length()))
.putHeader(HttpHeaders.CONTENT_TYPE,"application/x-www-form-urlencoded").write(buffer);
            request.end(); 

我基本上是在使用 Azure 进行身份验证,对于响应,我应该得到一个带有令牌和其他信息的 JSON。使用纯 Java 可以,但我们需要让它与 Vertx 一起使用。

编辑 - 此代码有效 - 我得到 JSON,但不是顶点

String url = "https://login.microsoftonline.com/common/oauth2/token";
URL obj = null;
obj = new URL(url);
HttpsURLConnection con = null;
con = (HttpsURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Host", "login.microsoftonline.com");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

// Send post request
con.setDoOutput(true);
DataOutputStream wr = null;

wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = 0;
responseCode = con.getResponseCode();

System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);

BufferedReader in = null;
in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));

String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}

in.close();

//print result
System.out.println(response.toString());

【问题讨论】:

  • 您能否编辑您的问题以使用 JDK HttpClient 显示您的代码,以便我们进行比较?
  • 你从哪里得到 404?你是从login.windows.net 那里得到的吗?
  • System.out.println(res.statusCode());给我 404,但我不确定请求是否到达 login.windows.net.... 我不确定它是否发送到我的网络之外。
  • @tsegismont 问题已编辑
  • 为了完整起见,在您的示例中,您使用了 2 个不同的 URL,这是预期的吗?

标签: java azure post http-post vert.x


【解决方案1】:

问题似乎是由于在未启用 SSL 并指定 443 端口的情况下请求 HTTPS url 引起的。 Vert.x httpclient 默认支持 HTTP 请求访问 Web 主机的 80 端口。您需要通过HttpClientOptions为httpclient启用SSL支持。

请尝试使用下面的代码而不是您的代码。

HttpClient client = vertx.createHttpClient(new HttpClientOptions().setSsl(true).setTrustAll(true));
HttpClientRequest request = client.post(443, "login.windows.net", "/common/oauth2/token").handler(res->{
                System.out.println(res.statusCode());
     }).putHeader(HttpHeaders.CONTENT_LENGTH,String.valueOf(buffer.length()))
.putHeader(HttpHeaders.CONTENT_TYPE,"application/x-www-form-urlencoded").write(buffer);
request.end(); 

作为参考,请查看官方文档http://vertx.io/docs/vertx-core/java/#_using_https_with_vert_x和GitHub中的代码示例https://github.com/vert-x3/vertx-examples/blob/master/core-examples/src/main/java/io/vertx/example/core/http/https/Client.java

【讨论】:

  • 好的,不错。如果您指定绝对 URL,您似乎需要使用 client.postAbs("https://login.windows.net/common/oauth2/token")。否则 Vert.x HttpClient 将字符串参数视为相对 URI,并连接到默认的 HttpClient 主机和端口。
猜你喜欢
  • 1970-01-01
  • 2015-02-20
  • 2013-09-09
  • 2013-09-19
  • 2013-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多