7

I have been using a WCF (.svc) service for a while for which request format is JSON and response format is XML in an Android application which is working fine. Couple of days ago, I implemented a certificate for SSL purpose on the WCF service from DigiCert (using my wildcard capabilities). The service is accessible from browser and shows no error. Below is the WebConfig

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="IntermediateWebService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
      <customErrors mode="Off"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
          <behavior name="IntermediateWebService.WebBehavior">
              <serviceMetadata httpsGetEnabled="true" />

              <serviceDebug includeExceptionDetailInFaults="true"/>
          </behavior>
        <behavior>

          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="WebBehavior">
            </behavior>
        </endpointBehaviors>
    </behaviors>
      <bindings>
          <basicHttpBinding>
              <binding name="secureHttpBinding" maxBufferPoolSize="524288" maxReceivedMessageSize="999999999">
                  <security mode="Transport">
                      <transport clientCredentialType="None"/>
                  </security>
              </binding>
          </basicHttpBinding>
          <webHttpBinding>
              <binding name="webHttpBindingConfig">
                  <readerQuotas maxStringContentLength="2048000" />
              </binding>
          </webHttpBinding>
      </bindings>
      <services>

          <service behaviorConfiguration="IntermediateWebService.WebBehavior" name="IntermediateWebService.Service1">
              <host>
                  <baseAddresses>
                      <add baseAddress="https://myurl:4445/"/>
                  </baseAddresses>
              </host>
              <endpoint address="" binding="basicHttpBinding" contract="IntermediateWebService.IService1" behaviorConfiguration="WebBehavior" bindingConfiguration="secureHttpBinding" />
          </service>
          <!--<service name="IntermediateWebService.Service1">
               <endpoint address=""
      binding="basicHttpBinding"
      bindingConfiguration="secureHttpBinding"
      contract="IntermediateWebService.IService1"/>



          </service>-->
      </services>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
 <system.webServer>
     <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

So now while using the same Android code, the response is always

The server cannot service the request because the media type is unsupported.

I have tried using SSL Factory and without it as well.

HttpClient client = getHttpsClient(new DefaultHttpClient()); //new DefaultHttpClient();

            HttpPost get = null;
            commandType = params[0].toString();
            if ("Login".equals(params[0])){

                JSONStringer img = new JSONStringer()
                        .object()
                        .key("value")
                            .object()
                                .key("username").value(params[1].toString())
                                .key("pwd").value(params[2].toString())
                                .key("channelID").value(params[3].toString())
                            .endObject()
                        .endObject();
                        StringEntity se = new StringEntity(img.toString());

                get = new HttpPost("https://" + serverIP + ":" + serverPort + "/Service1.svc/auth");
                //get.setHeader("User-Agent", "com.app.new");
                get.setHeader("Accept", "application/json");
                get.setHeader("Content-Type", "application/json");
                get.setEntity(se);
koto
  • 720
  • 2
  • 9
  • 29
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
  • 1
    Try some of my answers at http://stackoverflow.com/questions/32154115/android-volley-self-signed-https-trust-anchor-for-certification-path-not-found/32219177#32219177 or http://stackoverflow.com/questions/32051482/how-can-i-set-signalr-in-android-studio-to-ignore-ssl-issues-for-delvelopement/32051885#32051885 or http://stackoverflow.com/questions/32969952/android-to-server-communication-using-ssl-with-bouncy-castle/32980130#32980130 to see if they can help or not – BNK Apr 11 '16 at 04:00
  • Is the tls version supported on the target android platform? – QiMata Apr 12 '16 at 00:13
  • Try this answer. http://stackoverflow.com/questions/36492885/get-wcf-rest-get-request-in-android – Ishan Fernando Apr 12 '16 at 14:04
  • try with using this header for requests "Content-Type", "text/xml; charset=utf-8" – Mehdi Apr 12 '16 at 18:35
  • Have you tried making a request from something other than the browser and seen it working? For instance if you're using Google Chrome, try the addon called "Postman RestClient" og "Advanced RestClient". See if it works with the same headers and `POST` body as the ones you set in your Java code. What I'm trying to get at, figure out if this is an issue with Android/Java or not. If it's still working, try to replace your HTTP client with `OkHttp` for instance and see if that makes a difference. You could also expose your service to us and we could "test-drive" it if you like ;-) – Darwind Apr 12 '16 at 21:21
  • http://stackoverflow.com/questions/34977911/android-post-request-to-wcf-service – Muhammad Shahzad Apr 13 '16 at 05:01

1 Answers1

1

As I explored the error returned with status code 415, the request somehow was not accepted by the WCF service as valid JSON, therefore by adding the following attribute in svc markup did the trick

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

Here is the complete markup

<%@ ServiceHost Language="C#" Debug="true" Service="IntermediateWebService.Service1" CodeBehind="Service1.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50