Error 406 Not Acceptable with idHTTP on Android

孤街浪徒 提交于 2019-12-02 00:06:12

TIdHTTP works exactly the same way on all platforms, as Indy uses a single cross-platform codebase. So the generated HTTP request should be exactly the same on all platforms.

An HTTP 406 error happens when the HTTP request includes an Accept header that does not specify any media type that the server is capable of rendering the response in. Per RFC 2616 Section 14.1:

If no Accept header field is present, then it is assumed that the client accepts all media types. If an Accept header field is present, and if the server cannot send a response which is acceptable according to the combined Accept field value, then the server SHOULD send a 406 (not acceptable) response.

Your PHP script is sending a text/plain response, so if you send an Accept header that does not allow text/plain then that can cause a 406 error. It sounds like Hostgator is enforcing that more than Hostinger does.

By default, TIdHTTP sets its Request.Accept property to the following string value:

'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'

Which technically allows all media types via */*, but just with a lower priority than some other media types. But that default should still be enough to allow a text/plain response, if the server implements Accept handling correctly.

You need to contact Hostgator and discuss the issue with them, as the problem is on their end, not yours.

That being said, since you know the server response is always text/plain, you could just add the following to your code before calling Post():

HTTP.Request.Accept := 'text/plain';
HTTP.Request.AcceptCharset := 'utf-8';

After long hours trying to solve, this is what happened.

On Windows, the application was working fine, and when I tried using HTTP.GET i imagined that couldn't be on server's end the problem, but on my request. So then, i started opening the headers of request and response both on Windows and Android using this code:

Astr.Add('Response text: ' + Http.Response.ResponseText);
Astr.Add(#13);  
Astr.Add('Raw Headers Response: ' + HTTP.Response.RawHeaders.Text);
Astr.Add(#13);
Astr.Add('Raw Headers Request: ' + HTTP.Request.RawHeaders.Text);

This was the message i've got on Android:

Response text: HTTP/1.1 406 Not Acceptable

Raw Headers Response: Server: nginx/1.10.2
Date: ...
Content-Type: text/html;
charset=iso-8859-1
Content-Length:226
Connection: keep-alive

Raw Headers Request: Connection: keep-alive
Content-Type: multipart/form-data; boundary=------(somenumbers)
Content-Length: 0 Host: myhost Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Encoding: identity
User-Agent: ...

And this was the message on Windows

Response text: HTTP/1.1 200 OK

Raw Headers Request: Server: nginx/1.10.2 Date: ... Content-Type: application/xml Connection: close Vary: Accept-Encoding,User-Agent

Raw Headers Request: Connection: keep-alive

Content-Type: multipart/form-data; boundary=--------(numebrs) Content-Length: 0 Host: myhost Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 User-Agent: ...

The same app on different platforms were sending different headers on Request. On Android, the idHTTP was sending an Accept-Encoding that on Windows was not. So I tried adding this: Http.Request.AcceptEncoding:= '*'; before the HTTP.POST and it worked! I received the xml just as expected.

I don't know why the idHTTP was changing the Request Accept-Encoding, but I had to specify another one and I chose this one because of MDN definition:

*

Matches any content encoding not already listed in the header. This is the default value if the header is not present. It doesn't mean that any algorithm is supported; merely that no preference is expressed.

I was able to resolve this issue by simple changing user agent string. I think remote machine(server) has some security installed.

I just set the Request -> UserAgent to:

Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0

and now it works !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!