EndRead failed with Xamarin.Forms HttpClient - httpclient

I define the httpClient using ModernHttpClient
var httpClient = new System.Net.Http.HttpClient(new NativeMessageHandler());
httpClient.DefaultRequestHeaders.ConnectionClose = true;
And then use the same httpClient instance for several requests
var result = await httpClient.GetAsync("someaddress");
This request fails sometimes but not always with IOException with message.
"EndRead failed". What is the general cause for this exception? Is the connection closed before its done or what? The code is running on Android.

Related

cant set Authorization Header in HttpClient ( .Net core 3.1 )

I am migrating from .net core 2.2 to 3.1. I am making an XUnit test method to test my controllers.
I successfully made and tested in .net core 2.2 projects, but after migrating to 3.1 it seems it cant set authorization header to my request so I am getting UnAuthorized from my app.
this is my Code :
[Fact]
public async void InvalidId_UnSuccessFull_GetById()
{
// Arrange
var httpClient = new HttpClient();
var token = await GetAdminAccessToken(); // Sends a login request and fetch a valid token
// httpClient.DefaultRequestHeaders.Add("Authorization",$"Bearer {token}");
httpClient.DefaultRequestHeaders.Authorization=new AuthenticationHeaderValue("Authorization",$"Bearer {token}");
var id = Guid.Empty;
// Act
var response = await httpClient.GetAsync("localhost:5000/Admin/User/{id}");
var message = await ExtractMessage(response);
// Assert
Assert.Contains(PersianErrorMessage.InvalidUserId, message);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
I debugged into httpClient class till the sendAsync method and the HttpRequestMessage request instance does not have an Authorization Header that I set above! What's wrong with my code?
The AuthenticationHeaderValue should be set like this:
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
Source: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.headers.authenticationheadervalue.-ctor?view=netcore-3.1#System_Net_Http_Headers_AuthenticationHeaderValue__ctor_System_String_
There are a couple of issues that could trip you up here. This can be a frustrating issue, so hopefully, this helps. The HTTP client is more than likely sending the Authorization header. If there is any kind of redirect, the Authorization header does not travel with it. I ran into this issue once and the only problem was my original URL did not have a trailing slash at the end.
What I entered:
https://api.example.com/v3/endpoint
Server endpoint:
https://api.example.com/v3/endpoint/
Step 1, Determine if there is a redirect.
Step 2, Make your URL match the final URI exactly
Clients like curl do this automatically. In .Net 3.1, you will have to make sure your client checks for redirects. A 401 error is actually a good thing--it means you more than likely reached the right endpoint albeit without the authorization token.
You can do this in code, but just run debug and break after you get the response. Examine the RequestURI property of the RequestMessage object of the HttpResponseMessage (response.RequestMessage.RequestURI). Compare this with your initial URL. If they do not match exactly, you've been redirected and the Authorization header was lost along the way.

vertx timeout if async result is failed

I am seeing a timeout in the browser when the server-side service ends in a failed result. Everything works fine if the service call succeeds but it seems as though the browser never receives a response if the call fails.
My service passes a result handler to a DAO containing the following code:
final SQLConnection conn = ar.result();
conn.updateWithParams(INSERT_SQL, params, insertAsyncResult -> {
if (insertAsyncResult.failed()) {
conn.close();
resultHandler.handle(ServiceException.fail(1, "TODO"));
} else {
resultHandler.handle(Future.succeededFuture());
}
});
I'm not sure where to go from here. How do I debug what the framework is sending back to the client?
The problem was that I needed to register a ServiceExceptionMessageCodec in an intermediate Verticle, one that was sitting between the browser and the Verticle that was performing the database operation.

SockJS eventbus client in Java for Vert.x

is there a Java SockJS client for Vert.x available?
Similar to the TCP/IP bridge, but based on SockJS.
Reason is that we want a unified protocol stack, connecting clients
to Vert.x. For JavaScript we can use vertx3-eventbus-client, which work great.
We are looking now for a similar solution for Java.
There isn't yet (work-in-progress). However you can write a basic client yourself using the Vert.x HttpClient:
open a websocket
send pings periodically to prevent the connection from being closed
register a handler
listen for messages
Here's an example:
client.websocket(HTTP_PORT, HTTP_HOST, "/eventbus/websocket", ws -> {
JsonObject msg = new JsonObject().put("type", "ping");
ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));
// Send pings periodically to avoid the websocket connection being closed
vertx.setPeriodic(5000, id -> {
JsonObject msg = new JsonObject().put("type", "ping");
ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));
});
// Register
JsonObject msg = new JsonObject().put("type", "register").put("address", "my-address");
ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));
ws.handler(buff -> {
JsonObject json = new JsonObject(buff.toString()).getJsonObject("body");
// Do stuff with the body
});
});
If you need to work with different addresses then your handler will have to inspect the JSON object, not just get the body.

same HttpClient multiple request ends up with different HttpSession

I am using the new Apache HttpClient 4.2 (not the one from Apache Commons).
I need to open up one HttpClient and make multiple requests to the same server. From the documentation, the httpClient should automatically maintain the cookie, and therefore have the multiple requests fall in the same session. However, on the server side, I am debugging thru and see that
HttpSession session = req.getHttpSession(true);
is returning a new HttpSession Object every time.
my client code is like this.
// 1st time
HttpClient httpClient = new DefaultHttpClient();
req.getSession(true).setAttribute(HTTPCLIENT, httpClient);
HttpGet httpget = new HttpGet(redirectUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String content = httpClient.execute(httpget, responseHandler);
// subsequent calls
HttpClient httpClient = getHttpClient(req);
HttpGet httpget = new HttpGet(redirectUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String content = httpClient.execute(httpget, responseHandler);
// supported by the private method
private HttpClient getHttpClient(HttpServletRequest req){
return (HttpClient) req.getSession(true).getAttribute(HTTPCLIENT);
}
did I do anything wrong?
my dumb experiment.
it was because I was mixing up using and by passing the HttpClient on different requests from the same browser (e.g. by passing on .js and .css files). These by passed resources returned a different sessionId to the browser, and subsequently, the browser started using the new sessionId.

gwt getModuleBaseURL on same server creates an java.lang.UnsatisfiedLinkError

I have a single GWT web-application integrated with Spring MVC. I have a working Controller which works perfectly and is unit tested to accept POSTed JSON data and returns JSON data.
From within the same application, to avoid any SOP cross-site domain issues, I am making a call with a RequestBuilder to POST the same json data, and I expect JSON data back.
I created a basic java class that should make a call, but I have a few issues. This running web-app is running in hosted mode in Jetty in Eclipse. I have done a ton of research on how GWT should make a call to an existing web-service with a simple HTP request.
The first issue from my unit test is that:
String baseUrl = GWT.getModuleBaseURL();
is not working and I get:
java.lang.UnsatisfiedLinkError: com.google.gwt.core.client.impl.Impl.getModuleBaseURL()Ljava/lang/String;
I think I know what the correct URL should be, so when I hard-code the url correctly, and execute this code:
String url = getRootUrl() + "rest/pendingInvoices/searchAndCount";
System.out.println("PendingInvoiceDataSource: getData: url=" + url);
// Send request to server and catch any errors.
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
builder.setHeader("Accept", "application/json");
builder.setHeader("Content-type", "application/json");
// builder.setRequestData(requestData);
try
{
System.out.println("PendingInvoiceDataSource: SEND REQUEST: getData: requestData=" + requestData);
Request request = builder.sendRequest(requestData, new RequestCallback()
{
public void onError(Request request, Throwable exception)
{
System.out.println("Couldn't retrieve JSON");
}
#Override
public void onResponseReceived(Request request, Response response)
{
if (200 == response.getStatusCode())
{
// updateTable(JsonUtils.safeEval(response.getText()));
System.out.println("data=" + response.getText());
}
else
{
System.out.println("Couldn't retrieve JSON (" + response.getStatusText() + ")");
}
}
});
}
catch (RequestException e)
{
System.out.println("Couldn't retrieve JSON");
}
I get this error on he sendRequest:
java.lang.UnsatisfiedLinkError: com.google.gwt.xhr.client.XMLHttpRequest.create()Lcom/google/gwt/xhr/client/XMLHttpRequest;
at com.google.gwt.xhr.client.XMLHttpRequest.create(Native Method)
at com.google.gwt.http.client.RequestBuilder.doSend(RequestBuilder.java:383)
at com.google.gwt.http.client.RequestBuilder.sendRequest(RequestBuilder.java:261)
I think this might be a quick fix, or maybe something small I have forgotten, so I'll try some more testing and see what I can find.
Everything client in GWT is only meant to run on the client-side: compiled to JS or in DevMode.
Only shared, server and vm classes can be used on the server-side.
If you want to get your server URL, use the appropriate methods from the HttpServletRequest (or whatever it is in Spring MVC as it seems from how you tagged the question that's what you're using).
If you want to make HTTP requests from your server, use an HttpURLConnection, or OkHttp, Apache Http Components or similar libraries, or even Spring's own HTTP client API.
Actually, it was only the Unit Test that was having a problem. Once I actually tried to run the deployed code, it all worked. I'll still try h GWTTestCase as suggested in order to get the unit test working.
But everything worked correctly when I ran the deployed code.
I also changed: String baseUrl = GWT.getModuleBaseURL();
which gave me:
http://localhost:8888/MyProject
To: String baseUrl = GWT.getHostPageBaseURL();
which gave me:
http://localhost:8888/
and that all worked.