Retrofit 2 How to get created request string before that request is being sent to the server? - retrofit

I am using httpClient.addInterceptor(logging); to log every requests made before it is being sent to the server.
But, I have to use those requests to be saved as a string for my generous purpose. If it is possible than how can I implement it?

Related

Why should a client state http method?

We know the difference between POST and GET, but why should a client state the method type when issuing http requests? Why should it make a difference for the server? in the end, it is the server job to deal with those requests according to their URL and Content. either by redirecting, blocking or accepting and using data (existing in the URL or request body).
An endpoint can accept both GET and POST requests (along with PUT, PATCH and DELETE). If the client does not explicitly state what type of request they are sending, the server will interpret it as a GET request (the default).
Consider the following PHP example, sitting on https://api.example.com/resources/:
<?php
if ($_POST["request"]) {
// Create new resource
}
else if ($_GET["request"]) {
// List existing resources
}
In both instances, the request parameter is sent to the same page, and different logic is run based on what the method is. But considering the same data is sent to the same page in both instances, the server wouldn't know which one of the two conditions to step into if the client doesn't explicitly specify the method.
In RESTful programming, both the client and server have been programmed to understand the request, but the client has no knowledge of the server itself. It is up to the server to process the request, based off of what the client asks it to do. And the client asks it to do different things by specifying the method.

Simple HTTP call without opening Browser

Hello everybody I'm trying to do a simple HTTP call to a Tomcat Server running on my server from my Android App. The server will then execute a certain command to my website. I created a button that when I click it runs the HTTP call from the App.
If I use the approach below, it opens the browser on my phone to run this HTTP. Is it possible to do something similar but not have my app open the browser???
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + IP + ":8080/server/run.jsp"));
startActivity(browserIntent);
thank you so much in advance :D
Of course it starts your browser. Your code is explicitly asking Android to launch an app that can "view" the URL.
If you want your app to access the URL directly, use HttpURLConnection instead:
1.Obtain a new HttpURLConnection by calling URL.openConnection() and casting the result to HttpURLConnection.
2.Prepare the request. The primary property of a request is its URI. Request headers may also include metadata such as credentials, preferred content types, and session cookies.
3.Optionally upload a request body. Instances must be configured with setDoOutput(true) if they include a request body. Transmit data by writing to the stream returned by getOutputStream().
4.Read the response. Response headers typically include metadata such as the response body's content type and length, modified dates and session cookies. The response body may be read from the stream returned by getInputStream(). If the response has no body, that method returns an empty stream.
5.Disconnect. Once the response body has been read, the HttpURLConnection should be closed by calling disconnect(). Disconnecting releases the resources held by a connection so they may be closed or reused.

SignalR: requesting cookie before sending each SignalR message

Situation: For a web application using SignalR, for sending messages from server(an Azure WebRole) to a browser, I need to access a browser cookie before sending each message to the browser. The cookie would contain some info to process/decode the message before sending it (I can't do this in java script, due to access complexities of decode algo).
The scenario would be to have the access to the cookie as soon as the message is ready in the server --> decode the message --> send the message.
Question : Does it make sense to ask the browser to give me the cookie before each message that I want to send to the browser? Or Does it ruins the whole idea of SignalR (being a always connected tcp)?
If it does make sense, then how would I go about this? How can I access cookie in the Hub?
If this would not be possible then I'd have to store the decode information in some persistent storage in the server.
P.S: I'm pretty new to SignalR, so pardon me if the question is too naive.
Cookies seem like a reasonable way to communicating immutable data from the client to the SignalR server. You can read a cookies from inside a Hub as follows:
public void MyHubMethod()
{
Cookie cookie = Context.Request.Cookies["cookieName"];
// ...
}
Just be aware that the request referenced by Context.Request will be very long running in the case of WebSockets, so don't expect to pick up any changes that may be made to cookie after you initiate the SignalR connection.

Handling bad request in asp.net web api

I have a api url like below in my mvc4 app
http://localhost:15839/api/mydata/getdata/3365895543/PROBLEMDATA/myotherparam
Now client is consuming the above url to send httprequest. In response api is sending back a response. but in PROBLEMDATA of the url user is sending bad characters that are giving me Bad Request - Invalid URL. I can't force my client source to encode data. i need to handle it in my web api and give back my client a string "Unsucessful". I have seen this webapi cycle wondering at which point I should handle this. probably at http message handler but How?
I may need to follow this. but Register(HttpConfiguration config) also doesn't get hit
I believe you can capture this globally by overriding the application_error method. From there I suppose you could produce the "unsucessful" response or pass the request along to be handled at the controller level.
Take a look at this question as well.

Saving client address to response again and again

How can I save the client's complete address in asp application? Actually I want to send a response multiple times to same client without new call request by him. I don't want to end the response, when he called first time a service.

Resources