Reputation: 383
I don't know why, but the post that I do with Vertx is simply not working. All the time the error is 404.
The same link and body I used with pure Java, and I got the response from the server. What do I do wrong?
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();
I am basically doing authentication with Azure, and for the response I should get a JSON with token and other info. With pure Java, works, but we need to make it work with Vertx.
EDIT - this code works - I get JSON back, but is not vertx
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());
Upvotes: 0
Views: 2234
Reputation: 24138
It seems that the issue was caused by requesting a HTTPS url without enable SSL and specify the 443 port. Vert.x httpclient default support HTTP request to access the port 80 of a web host. You need to enable SSL support for httpclient via HttpClientOptions
.
Please try to use the code below instead of yours.
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();
As references, please view the offical doc http://vertx.io/docs/vertx-core/java/#_using_https_with_vert_x and the code sample in GitHub https://github.com/vert-x3/vertx-examples/blob/master/core-examples/src/main/java/io/vertx/example/core/http/https/Client.java.
Upvotes: 3