Why does the webflux client give a 404? - reactive-programming

I'm trying to make a webflux client to connect to a remote websocket. There is an example websocket located at https://www.websocket.org/echo.html. I can have my browser make a wss request there simply by pressing "connect". In my browser developer toolbar I can then see that a succesful request is made to wss://echo.websocket.org/?encoding=text
This url is also mentioned in https://stackify.com/reactive-spring-5/ (the code there doesn't work because "input" is not defined).
However, when I try to access the same url from spring boot 2.0.0 with webflux, I get a 404:
#PostConstruct
public void run() throws URISyntaxException {
WebClient webClient = WebClient.create();
Flux<String> r =
webClient.get().uri("wss://echo.websocket.org/?encoding=text")
.retrieve()
.bodyToFlux(String.class)
.log();
r.subscribe(res -> System.out.println("ok: " + res), ex -> System.out.println(ex));
}
error:
org.springframework.web.reactive.function.client.WebClientResponseException: ClientResponse has erroneous status code: 404 Not Found
My best guess is that it somehow does not work with urls that start with "wss://". Can I change the code so that the request will be successful?

WebClient is not a WebSocket client, ReactorNettyWebSocketClient (mentioned in the article you linked) is a WebSocketClient. Please use this instead.

Related

Strange issue with Vertx Http request

I configured an HTTPS website on AWS, which allows visiting from a white list of IPs.
My local machine runs with a VPN connection, which is in the white list.
I could visit the website from web browser or by the java.net.http package with the below code:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://mywebsite/route"))
.GET() // GET is default
.build();
HttpResponse<Void> response = client.send(request,
HttpResponse.BodyHandlers.discarding());
But if I replaced the code with a Vertx implementation from io.vertx.ext.web.client package, I got a 403 forbidden response from the same website.
WebClientOptions options = new WebClientOptions().setTryUseCompression(true).setTrustAll(true);
HttpRequest<Buffer> request = WebClient.create(vertx, options)
.getAbs("https://mywebsite/route")
.ssl(true).putHeaders(headers);
request.send(asyncResult -> {
if (asyncResult.succeeded()) {
HttpResponse response = asyncResult.result();
}
});
Does anyone have an idea why the Vertx implementation is rejected?
Finally got the root cause. I started a local server that accepts the testing request and forwards it to the server on AWS. The testing client sent the request to localhost and thus "Host=localhost:8080/..." is in the request header. In the Vert.X implementation, a new header entry "Host=localhost:443/..." is wrongly put into the request headers. I haven't debug the Vert.X implementation so I have no idea why it behaviors as this. But then the AWS firewall rejected the request with a rule that a request could not come from localhost.

Getting connection timeout error while calling get access token Microsoft Graph API through REST POST call

I am trying to get access token from https://login.microsoftonline.com/{tenentname}/oauth2/v2.0/token endpoints though HttpClient post request. Applied required 4 parameters/headers with the same.
But I am getting connection timed out. Connection will be retried using another IP address (after trying with 7 different IP address) getting Shutdown connection error. Connection discarded.
Please find below code snippet.
HttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
HttpPost post = new HttpPost("https://login.microsoftonline.com/{tenentname}/oauth2/v2.0/token");
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
urlParameters.add(new BasicNameValuePair("client_id", {id_value}));
urlParameters.add(new BasicNameValuePair("client_secret", {secret_value}));
urlParameters.add(new BasicNameValuePair("scope", "https://graph.microsoft.com/.default"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
post.setHeader("Content-type", "application/x-www-form-urlencoded");
ClosableHttpClinet httpClient = HttpClients.custom().setConnectionManager(poolingConnManager).build();
ClosableHttpResponse response = httpClinet.execute(post);
System.out.println(EntityUtils.toString(response.getEntity()));
Same its worked with Postman without any proxy setting, I am getting proper response in Postman.
Please assist on the same.
Instead of using HTTPClient, i tried with Microsoft Graph Client (you can download from NuGet) and it worked for me. So i would suggest you to follow the steps.
Install the SDK
Configure the GraphClient
Make the call.
Also with specific to ivy dependency, here's the related thread.

RESTful call testing in Eclipse

In my case, I am running a eclipse project providing the Restful api, and I will call that api like in the following example. I am curious if I should create another project in the eclipse to run the following code to test the api.
Jersey Example
Form form = new Form();
form.add("x", "foo");
form.add("y", "bar");
Client client = ClientBuilder.newClient();
WebTarget resource = client.target("http://localhost:8080/someresource");
Builder request = resource.request();
request.accept(MediaType.APPLICATION_JSON);
Response response = request.get();
if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
System.out.println("Success! " + response.getStatus());
System.out.println(response.getEntity());
} else {
System.out.println("ERROR! " + response.getStatus());
System.out.println(response.getEntity());
}
you can run your code as a java application using main method.
Or since you are saying you have you services running you can use POSTMAN REST Client available as a plugin for chrome and chromium browsers. i don't know about the support for the same in other browsers.
Using postman you'll be able to see exactly how your restful service is working. you'll be able to send request headers and other parameters as a part of the rest request. Postman is the way to go for end to end web service testing.

Authentication from NativeScript App(android/ios) to Sharepoint 2013 REST API

I'm trying to call Sharepoint 2013 REST API from an application developped with NativeScript(android/ios).
Using xhr or fetch NativeScript module I'm not able to authenticate correctly and call rest api.
Using Java I'm able to connect to the sharepoint server and call Rest API without problem Using this maven dependency: org.apache.httpcomponents httpclient 4.4.1.
public class SharePointClientAuthentication {
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY),
new NTCredentials("username", "password", "https://hostname", "domain"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("http://hostname/_api/web/lists");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}
Any one know what's the equivalent to the java code with NativeScript or a way to get Windows authentication(NTLM) to work.
Thanks in advance.
Initially I thought this should be handled by the native HTTP library and exposed as a part of the XMLHttpRequest implementation in NativeScript, but then I discovered ntlm.js. It's a small library that takes care of the NTLM challenge-response for you.
I patched it up a bit, to get rid of the browser dependency and push some polyfills, and got it running. I put up a demo project here:
https://github.com/hdeshev/nativescript-ntlm-demo

Outgoing http requests not showing in Fiddler from java spring mvc web app

Hi I'm sending some http requests from a java spring mvc web app and when I have fiddler open I don't see any outgoing responses.
Using this code to send to an address similar to: http:///getstuff?stuff="whatever"
HttpGet httpget = new HttpGet(url);
log.info("Executing request " + httpget.getURI());
// Create a response handler...
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseBody = httpclient.execute(httpget, responseHandler);
Does anybody know why these outgoing calls are not showing up in fiddler?
Did you remember to configure your JVM to proxy its HTTP requests? http://fiddler2.com/documentation/Configure-Fiddler/Tasks/ConfigureJavaApp