Trying to implement gzip on an Android project to reduce client data charges. All devices connect to a WCF webservice and IIS is now sending compressed data back to the devices as expected. Now I need to work out how to post back gzipped xml data modified on the android device.
The device code is as follows
httpsURLConnection.setDoInput(true);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setConnectTimeout(TIMEOUT);
httpsURLConnection.setReadTimeout(TIMEOUT);
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setRequestProperty("Content-Type", "application/xml");
httpsURLConnection.setRequestProperty("Content-Encoding", "gzip);
httpsURLConnection.connect();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new BufferedOutputStream((httpsURLConnection.getOutputStream())));
gzipOutputStream.write(_xmlText.getBytes());
gzipOutputStream.flush();
gzipOutputStream.finish();
gzipOutputStream.close();
Running Wireshark on webserver shows the gzip packets and decompresses them to show the correct data from the device.
The problem is that the WCF web service does not seem to recognise the data - the error is XmlException - The data at the root level is invalid. Line 1, position 1.
Which makes me think that the data is still compressed and WCF cannot handle gzip - which I seem to remember reading about previously. Do I then need to create a message decoder in .net to handle the gzip compression? Was this hopefully addressed in .net 4.5?
Any help with these questions appreciated.
Related
I programming python server and android client.
program's logic is client send multiple file to server.
I first try C/C++ socket server but receive error. so I change python. because server on raspberry pi.
I have to implement file upload, audio streaming. so I think this logic.
1. Client send http request to server
2. When server receives the request, server create tcp socket and listen.
3. Client receives success response, connect to server and file upload.
Audio streaming will implement similar way.
Is it ok to implement this way? or is there a better way?
Please give me a hint how to implement it.
Ignore socket as much as you for small deployments. They are little more complicated to handle.
Now asuming you want to upload a image file or some other file to a python you can use Flask Upload
Next move on to audio, if you have to upload audio from client to server, than there is no need to stream or stuff, just pass appropriate MIME type during upload.
ALLOWED_AUDIO_EXTENSIONS = set(['wav', 'ogg', 'mp3', ])
def audio_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_AUDIO_EXTENSIONS
if file and audio_file(file.filename):
filename = secure_filename(file.filename)
#perform some application logic with audio files and then save them in file system or call boto3 to save on s3
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
Code is modified from flask examples.
I am currently working on a project whereby i need to send a request from an android application to my Arduino mega which in turns response back with the states of LEDs connnected to the Arduino. I have already implemented the GET request. However i need to know how to read the response back and how to send the string from the Arduino to the Android app.
Here is the code for the response:
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
client.println("{\"status\":\"ok\"}"); // <--- how to create the string?
and also how to decode it from the android application. I have seen tutorials on the internet about using InputStream to do so but am unable to understand the code. Could you please help me on this.
You have to implement a Webserver on Arduino side and use a HTTP library (i.e. OkHTTTP) to connect to the Webserver.
I used OkHTTP on Android side but you could use other libraries too.
This link is something like you are looking for. It is from my blog.
Otherwise you can use aRest. It is very simple to use. If you want to know more give a look at this link from my blog.
I have a quite simple problem but I've still couldn't find a solution yet.
What I want to accomplish:
I'm trying to establish a reliable connection between a smartphone running Android and the ESP8266 wifi module. I would like to send short HTTP string messages, where the phone plays the role of a client and the ESP8266 of a server. For managing HTTP requests I'm using the Volley library.
What already works:
I am able to do a successful HTTP GET request to the ESP8266 from a browser running on the Android phone. I also managed to use Volley to make a GET request to a server running on the web.
What doesn't work:
On the other hand, I cannot successfully send a GET request to ESP8266 using Volley. To be more precise, I get an EOFException when the server (ESP8266) tries to close the connection after it has responded. When using a browser the body of the response gets displayed after the connection is closed but in case of Volley the connection closing fails and shuts down the server.
I have no idea how to solve this problem/bug. What frustrates me is that the same commands for sending a HTTP response on the ESP work well when using a web browser but fail when using Volley. So I guess the problem is something about Volley.
Any ideas why Volley throws such exception? Any help would be deeply appreciated.
I am trying to test the push response from a server which support HTTP2 Server with an android app (Os=Android KitKat) . The server push another ressources(pictures) after a http request for index.html.
I don't know how to access to push stream (picture). I have done capture on server and it send the push stream. But the client (Okhttp 2.1.0) always send a frame RST_STREAM and just return the index page content .
I just start use okhttp and would like to know how to acces push stream from Okhttpclient response ? I have read that that it exist API for handle push stream from HTTP2. is it available in Okhttp 2.1.0 or Okhttp 2.2.0?
Thanks.
OkHttp doesn't implement pushed streams yet. When we do, it'll likely be to push into the cache only.
I have a ASP.NET website deployed to IIS with a couple of ashx that returns JSONs to be consumed by an Android application.
I have implemented an authentication logic using Basic Authentication.
The problem: When accessed from Android, the server response is a 400 Bad request. The httperr log file says "400 - Hostname -".
It works when I try it out on localhost from Android emulator
It works when accessing the ashx file on the server from a browser
It works when replicating the call in Fiddler
(If I use Fiddler with the Android Emulator, the Response will be -1 (and looking in Fiddler at the raw data sent, it seems to loose the host from the url) - but this is another issue so don't dwell on that, i just thought I would mention it...)
Turns out my problem (and solution) was the same as in this thread:
HTTP POST request with authorization on android