Web service returns SOAP if content type is not specified

A few years ago, we developed the .NET web service for mobile applications. This service is called by iPhone / Android / Blackberry / WindowsPhone and all native applications developed by a third party. We added JSON support, so some applications access this service using JSON calls, and some use SOAP.

Webservice returns JSON only when the request is sent with the HTTP header Content-type: application/json .

We ran into a problem with one Android platform (in particular, Galaxy Nexus), where the Content-Type header is missing for GET requests. Our third-party application developer tried many solutions, but could not find a way to force Content-Type requests for GET.

However, we noticed that the Accept header was set correctly and sent, but I did not find a way to change the web service to use this header instead of the Content-Type to return JSON in these cases.

Here is an example request that comes up with an XML response, 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 an 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 a JSON response in some way or check the Accept header to do this? (Besides porting to WCF?)

If not, the application developer told me that they use the Spring framework to create HTTP requests. If there is a decision on how to make it work on the application side and force the Content-Type header to be sent for GET requests, it will also appreciate!

Thanks!

+4
source share
3 answers

Thanks for the answer.

Although setting the content type did not solve the problem, it pointed me in the right direction.

The following resolved this for me:

 [WebMethod( BufferResponse = false, CacheDuration = 0 )] [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json) ] public LogonResponse Logon(int system, string username, string password) { return SetOutput<LogonResponse>(service.Identify(username, password)); } 

and actual conversion:

  public static T SetOutput<T>(object response) { var accept = HttpContext.Current.Request.Headers["Accept"]; var ctype = HttpContext.Current.Request.ContentType; if (string.IsNullOrEmpty(ctype) && !string.IsNullOrEmpty(accept)) if (accept.ToLower().EndsWith("application/json")) { var serializer = new JavaScriptSerializer(); var json = serializer.Serialize(response); HttpContext.Current.Response.ContentType = "application/json"; HttpContext.Current.Response.Write(json); HttpContext.Current.Response.End(); } return (T)response; } 

Hooray!

+1
source

You can try this (cannot check at the moment).

 public LogonResponse Logon(int system, string username, string password) { string accept = HttpContext.Current.Request.Headers("Accept"); if (!string.IsNullOrEmpty(accept)) { if (accept.ToLower.EndsWith("application/json")) { HttpContext.Current.Response.ContentType = "application/json"; } } return service.Logon(system, username, password); } 

Edit: updated response request

+2
source

Use the following to also wrap it all with the expected d element:

  public static T SetOutput<T>(T response) { var accept = HttpContext.Current.Request.Headers["Accept"]; var ctype = HttpContext.Current.Request.ContentType; if (string.IsNullOrEmpty(ctype) && !string.IsNullOrEmpty(accept)) if (accept.ToLower().EndsWith("application/json")) { var wrapper = new JSONWrapper<T> {d = response}; var serializer = new JavaScriptSerializer(); var json = serializer.Serialize(wrapper); HttpContext.Current.Response.ContentType = "application/json"; HttpContext.Current.Response.Write(json); HttpContext.Current.Response.End(); } return response; } public class JSONWrapper<T> { public T d { set; get; } } 
0
source

Source: https://habr.com/ru/post/1434932/


All Articles