I have an android application and I am trying to make an http/2 call with okHttp version 3.13.1, in my case for one request I will be receiving two responses sequentially from the server, but with the okHttp client I am getting only one response and if I try to send the request using curl command I am receiving two responses sequentially from the server.
Is there a way to handle multiple responses for a single http/2 request in android?
I tried various ways, but no luck:( Would be more than happy, if anyone could provide your inputs.
Thanks for your help and time in advance!!!
I am basically sending a get request from my android application using okHttp client to a server which supports http/2, so expecting to receive 2 different response for that single request in a timely manner.i.e the second response will be sent from the server after 5 seconds gap of receiving the first response.
Below is my response :
First response
event:initialize
{"session_id":"df313001-6461-431c-bcc1-7cb931bda4f5","deviceId":"YL0012345678"}
Second response
event:voice_response
data:{"intents":[{"version":0,"intent":"telstra_intent_voice","params":{"voiceResponse":{"displayText":"Launching now","vuiFileName":"E03.01.P2.V01.E.wav"},"action":{"ecp":"/launch-install/71361"}}}],"deviceId":"YL0012345678","channelId":"269671"}
But, in my case once I receive the first response the client is no longer listing to the server. When I receive the first response, in my log I see :
com.example.okhttp3 D/OkHttp: <-- END HTTP (166-byte body),
so not receiving the second response.
Appreciate your help!
I found a workaround. You can send the response as multipart data. Then encapsulate all your responses into the multipart data seperated by 'boundary'. The multipart data will not end until you send the 'end mark'(0). Then the client side could decode all the responses one by one simultaneously when the server sending the multipart data.
The multipart data will looks like this:
--boundary
response_1
--boundary
response_2
...
--boundary--
0
there is also a demo here.
Related
TLDR: Is there a way to force OkHttp to correctly handle unexpected/unrequested 100 Continue HTTP responses?
I'm using OkHttp 3.8.1 on Android to POST to a poorly-behaved server.
Even though the request does not include an "Expect: 100-continue" header, the web server returns a 100 Continue response. Rather than continuing to send the request body, then getting the actual (200) response, OkHttp stops there and sends back the 100 Continue response in my okhttp.Callback.
I tried explicitly including "Expect: 100-continue" in the request to trigger OkHttp's logic, but the server (possibly due to some bug) claims the header is malformed and rejects the request. I also tried sending "Expect:" (no value), but the server still sends the 100 Continue response and OkHttp stops there.
Other HTTP clients (I've tested 3 so far) can talk to that server just fine. They handle the 100 Continue response correctly even though they didn't see "Expect: 100-continue" in the request header. Is there an option I can set, or an interceptor I can write, to make OkHttp do the same?
Sounds like something we can fix in OkHttp. This is weird and the server is broken, but if other clients handle it OkHttp should too.
Please report a bug there?
I have an established code base which successfully makes GET and POST requests to a server to download/upload data. We're looking to change the domain name and have set up a 301 HTTP redirect to go from the original domain to the new domain. When doing a GET request, it seems to automatically handle the redirect in the background and successfully retrieves the response. When I am sending a body via POST, however, it throws a java.net.ProtocolException: content-length promised xx bytes, but received 0.
Is there any way to ensure that the body is sent in the redirect request as well? Thanks for any help in advance!
I want my android app to periodically send location information to my django server.
Based on my research, android client should write the location in JSON, payload JSON in http post, and send http post request to django server periodically. and the http post request is created by the client directly, not from a post form by django. Is it a normal way?
If posting data without form is a general way, then to stop csrf verification, according to the answer of Android sending post requests to django server csrf failing, the android client should
"getting the token from the server and sending that along with the POST data "
(suppose I do not use solution of #csrf_exempt)
My question is where to get the csrftoken cookie?
I do see csrftoken cookie if I send a GET request to a post form, but if posting data directly (without asking for the post form), where does a client get the csrftoken cookie?
You could use an API like tastypie or rest-framework. The only way to get the token is by accessing the page that gives it.
In Android i'm sending multiple request to the server for each request server will send a response.Now my problem is identify for which request response is received.
please suggest someway of sending your HTTP request using a Tag.
You should use Synchronized for Receiving a Response.
I'm working on an issue which is:
+ I send a HTTP POST request to a server and get the response.
+ I use a DefaultHTTPClient instance to send the request. And I use BasicNameValuePair to add the params for the server to process.
There is a param which is a xml-based string. The problem is that, when I send an "empty" string like this: " ", the server response "SUCCESSFUL". But when I send another value which is much longer. The server response "UNSUCCESSFUL" due to this parameter.
Please tell me that whether Android has a max length limitation on BasicNameValue instance or not? And how can I fix this issue.
Thanks in advance.
I expect the problem is with your web service, not with the BasicNameValue implementation on Android: you are probably just sending it a bad parameter. Do you get SUCCESSFUL when you send the exact same request parameters from another programming language?