How to make a chunked transfer encoding HTTP request in Android? - android

How to generate a chunked transfer encoding HTTP request in Android ? The code below did not do the trick for me.
HttpURLConnection cn = (HttpURLConnection)(new URL(url)).openConnection();
cn.setRequestProperty("Transfer-Encoding", "chunked");

You can't request that the response is chunked. The server isn't even required to be able to generate chunked response.
What you are doing by sending that header, is telling the server that the content of the request is in chunked encoding.

Related

HTTP request Post to a Server where HTTP body is a JSON dictionary

I have a server I need to POST an http request to. The server is located at http://ec2-54-187-236-58.us-west-2.compute.amazonaws.com
I need to execute a POST /ios/search
I'm given that the HTTP body is a JSON dictionary but I cannot connect to the server itself. Nor am I able to see the JSON object in javascript when I enter the server url in a browser. I am using gson library in android to help me execute the http client request, but am getting the
IOException: Connection to http://ec2-54-187-236-58.us-west-2.compute.amazonaws.com/ios/search refused
What am I missing? Or is there an issue with the server url?

MultipartEntity Android always return server error

I have two days facing a problem sending Multipart form request that include data and images, and server always give me error "Unsupported Media Type"
I have tried 3 version of apache httpclient which are (4.1, 4.3, 4.5)
Here is my code
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.addTextBody("CategoryId", String.valueOf(categoryId), ContentType.TEXT_PLAIN);
multipartEntity.addBinaryBody("PrimaryImage", primaryImage, ContentType.create("image/jpeg") , "test.jpg");
I have added the following headers
request.setHeader("Accept", "application/json");
request.setHeader("Content-Disposition","form-data");
request.setHeader("Content-type","multipart/form-data");
request.setHeader("enctype", "multipart/form-data");
I am using POST Request with Digest Authentication
When I print the content of multipart form request I found the following data
--2gj-9i6ivyDT_6wE0ck0DTKUV_hwUu-6m7
Content-Disposition: form-data; name="CategoryId"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
1
--2gj-9i6ivyDT_6wE0ck0DTKUV_hwUu-6m7
Content-Disposition: form-data; name="PrimaryImage"; filename="test.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
����-�Exif
The server is keep sending me error and server admin told me that the server can't parse form-data and that the format of my request is wrong
can anyone help please
EDIT 1
When I remove the headers from my request and remove the image also the server is able to read all text parameter.
When I remove the headers from my request and keep sending then image I got SocketTimeoutException.
I don't know what is the problem

Android Compress data for posting to web server

I am still a beginner in Android. I am currently sending large amount of data(json) from the android app to web server using HTTPClient. I found that HttpURLConnection supports decompression for request but is there any support or any way that I can upload a compress/gzipped json to the web server?
Is the only way to achieve this is to manually gzip the json string and put the gzipped string into in to the post?
You can add GZIP compression in your HTTP header.
HttpUriRequest request = new HttpGet(url);
request.addHeader("Content-Encoding", "gzip");
// ...
httpClient.execute(request);
You can check this link for more information

HttpURLConnection not decompressing Gzip

I'm trying to use HttpURLConnection on Gingerbread+ Android devices and am having trouble with the gzip encoding. According to the documentation
"In Gingerbread, we added transparent response compression.
HttpURLConnection will automatically add this header to outgoing
requests, and handle the corresponding response:
Accept-Encoding: gzip"
The problem is that this is not actually happening. The Accept-Encoding: gzip header is not getting added at all. If I add it manually, I would then expect the decompressing part of it to work by connection.openInputStream() to automatically return a GZipInputStream but it just returns a plain InputStream.
Has anyone experienced this? I havent seen any posts of this happening so its very odd. The project is compiled against API 17 so that shouldnt be a problem and the device is running 4.3.
Thanks.
I had the same problem and it was related to HTTPS. If you call:
URL url = new URL("https://www.example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
you actually get an instance of HttpsURLConnection (a subclass of HttpURLConnection) that does NOT handle gzip automatically.
In those cases you can:
conn.setRequestProperty("Accept-Encoding", "gzip");
...
InputStream inStream = new GZIPInputStream(conn.getInputStream());
Since Android Gingerbread (http) and ICS (http + https), when you use Http(s)URLConnection, Android adds the Accept-Encoding: gzip header automatically, but only if you didn't add it yourself first.
In that case, if the server returns gzip-encoded content, you will automatically get a GZIPInputStream and the Content-Encoding: gzip header will be removed from the response. That's why it's called transparent: so the client cannot tell the difference between compressed or uncompressed responses by looking at the stream content or the headers.
If you add the Accept-Encoding: gzip header manually yourself however, then transparent gzip handling is disabled for that request and if the server returns gzip-compressed content, then you need to look for the Content-Encoding: gzip header and create the GZIPInputStream yourself to decompress content.
I tested with a few of my devices and HttpURLConnection is adding Accept-Encoding: gzip to the headers.
Have you tried configuring Fiddler for your Android devices to verify http headers? Perhaps your server does not support compression.
I've experienced this myself and also found that it worked if I added the header and used GZIPInputStream manually. However, we were setting our headers manually (had a custom header we had to add).
The Android documentation says "By default, this implementation of HttpURLConnection requests that servers use gzip compression." So I'm guessing that the default only applies if you don't manually set the headers.

Android Https signing parameters

Good day!
I have a trouble with sending https request from android application with signing parameters.
I have source which allows me to send secure http request, but authentification does not work.
I need to send request to some url with port number.
By using
HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
After this, I retrieve this parameters in string format like
param2=param1&param2=param2%20p - encoded format
Next I need take md5 hash and again introduce it in httpbody param without encoding
Example of request:
HTTP method - POST
Headers: Content-Length - 275
Content-Type - application/x-www-form-urlencoded
параметры в HTTP body -
PHONE=2583838&ORDER_STATE_ID=31&SOURCE=улица%20Жандосова,%20188&DESTINATION=Тепличная%20улица,%202а&CLIENTID=62&SOURCE_TIME=20130807120054&IS_PRIOR=true&NOTE=This%20is%20comment&PHONE_TO_DIAL=8(701)746-90-26&CUSTOMER=Anna%20Ahmatova&signature=ed4aaef27e2d46a46f449903f3524788
Help me please. This post request working but everytime I have trouble with authentification

Categories

Resources