We developed a .NET web service for mobile application several years ago. This service is being called by iPhone/Android/Blackberry/WindowsPhone and all native apps developed by a third party. We added support for JSON, so some apps access this service using JSON calls, and some using SOAP.
The webservice returns JSON only when the request is sent with HTTP header Content-type: application/json.
We encountered a problem with one Android platform (specifically the Galaxy Nexus), where the Content-Type header is missing for GET requests. Our third party app developer tried many solutions but could not find a way to force send the Content-Type for GET requests.
However, we did notice that the Accept header is set correctly and sent, but I found no way to change the web service to use that header instead of Content-Type to return JSON in those cases.
Here is the example request, which results with XML response, and not JSON as needed.
GET /mobile/service.asmx/Logon?system=2&username='test'&password='1234' HTTP/1.1
Accept: application/json
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
Connection: Keep-Alive
And excerpt from the webservice code:
[WebMethod(
BufferResponse = false,
CacheDuration = 0
)]
[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json) ]
public LogonResponse Logon(int system, string username, string password)
{
return service.Logon(system, username, password);
}
Is there a way to force JSON response in some way, or inspecting the Accept header to do so? (Other than migrating to WCF?)
If not, I was told by the app developer they use the Spring framework to make HTTP requests. If there’s a solution on how to make it work on the app side and force send the Content-Type header for GET requests, it’s also appreciated!
Thanks!
Thanks for the reply.
Although setting the content-type did not solve the issue, it did point me in the right direction.
The following solved it for me:
and the actual conversion:
Cheers!