none
Rest Service won't work over SSL HTTPS Please help!! Error 415 asp.net 4.0 RRS feed

  • Question

  • I am using .net framework 4.0. And using the visual studio 2017 community edition. The jquery script inside default.aspx  I use is the following:

    1. $("#btn").click(function () {
    2. $.ajax({
    3. type: "POST",
    4. contentType: "application/json",
    5. url: "Service1.svc/Hello",
    6. dataType: "json",
    7. data: JSON.stringify({ name: $("#fullname").val(), email: $("#email").val() }),
    8. success: function (result) {
    9. $("#result").val(result.d);
    10. }
    11. });
    12. });


    The Web.config in the root is configured to use https bindings and looks like the following:

    1. <?xml version="1.0"?>
    2. <configuration>
    3.  
    4. <system.web>
    5. <compilation debug="true" targetFramework="4.0" />
    6. </system.web>
    7. <system.serviceModel>
    8. <services>
    9. <service name="Bootstrap.Service1">
    10. <endpoint address="" binding="basicHttpBinding" bindingConfiguration="secureHttpBinding" contract="Bootstrap.Service1" />
    11. <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
    12. </service>
    13. </services>
    14. <bindings>
    15. <basicHttpBinding>
    16. <binding name="secureHttpBinding">
    17. <security mode="Transport">
    18. <transport clientCredentialType="None"/>
    19. </security>
    20. </binding>
    21. </basicHttpBinding>
    22. </bindings>
    23. <behaviors>
    24. <endpointBehaviors>
    25. <behavior name="Bootstrap.Service1AspNetAjaxBehavior">
    26. <enableWebScript />
    27. </behavior>
    28. </endpointBehaviors>
    29. <serviceBehaviors>
    30. <behavior name="">
    31. <serviceMetadata httpsGetEnabled="true" />
    32. <serviceDebug includeExceptionDetailInFaults="false" />
    33. </behavior>
    34. </serviceBehaviors>
    35. </behaviors>
    36. <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    37. multipleSiteBindingsEnabled="true" />
    38. </system.serviceModel>
    39. <system.webServer>
    40. <modules runAllManagedModulesForAllRequests="true"/>
    41. </system.webServer>
    42.  
    43. </configuration>

    The WCF file Service1.svc looks simple like the following:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Runtime.Serialization;
    5. using System.ServiceModel;
    6. using System.ServiceModel.Activation;
    7. using System.ServiceModel.Web;
    8. using System.Text;
    9.  
    10. namespace Bootstrap
    11. {
    12. [ServiceContract(Namespace = "")]
    13. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    14. public class Service1
    15. {
    16. [OperationContract]
    17. public string Hello(string name, string email)
    18. {
    19. return "Hello " + name + " your email is " + email;
    20. }
    21. }
    22. }

    When I run this code nothing happens I get a 415 error in return when I look in the development console in chrome,

    HTTP415: UNSUPPORTED MEDIA TYPE - The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.


    Under http everything was working perfect but as soon I changed the web.config a bit to use SSL and start the url with: https what works fine a get a nice green slot telling that the connection is safe.

    Please help me with this, and try to solve this for me.


    Thx in advance.

    WAW Hoefsloot

    Sunday, October 28, 2018 4:02 PM

All replies

  • Hi Wernad,

    The only thing we need to do is add the following the statement to the operation

    [WebInvoke(BodyStyle =WebMessageBodyStyle.Wrapped)]


    Complete code.

    [OperationContract]
            [WebInvoke(Method ="POST",UriTemplate ="Test/",BodyStyle =WebMessageBodyStyle.Wrapped,RequestFormat =WebMessageFormat.Json,ResponseFormat =WebMessageFormat.Json)]
            string Test(string Id);


    Feel free to contact me if the problem still exists.
    Best Regards
    Abraham

    Monday, October 29, 2018 4:27 PM
    Moderator
  • Hi Abraham,

    Thanks for your response.

    But I still keep the same error while I have added de script.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace Bootstrap
    {
        [ServiceContract(Namespace = "")]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class Service1
        {
            [WebInvoke(Method = "POST", UriTemplate = "Hello/", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            [OperationContract]
            string Hello(string name, string email)
            {
                return "Hello " + name + " your email is " + email;
            }
        }
    }
    

    HTTP415: UNSUPPORTED MEDIA TYPE - The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.

    So still not working sorry!

    I hope you want to look further for me.

    Monday, October 29, 2018 4:55 PM
  •    

    Hi,
    I am sorry that I didn’t seriously analyze your project structure. It seems that you add the SVC file in your webform project. and try to use the $.ajax method to invoke the method.
    I have made a demo,wish it is useful to you.
    SVC.

     [ServiceContract(Namespace = "")]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class Service1
        {
            [OperationContract]
            [WebGet]
            public string DoWork()
            {
                return "DoWork function";
            }
    
            [OperationContract]
            string Test(string Id)
            {
                return $"Hello {Id}";
            }
    }
      



    Web.config

    <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding name="mybinding">
              <security mode="Transport">
                <transport clientCredentialType="None"></transport>
              </security>
            </binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <endpointBehaviors>
            <behavior name="WebApplication1.Service1AspNetAjaxBehavior">
              <enableWebScript />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
          multipleSiteBindingsEnabled="true" />
        <services>
          <service name="WebApplication1.Service1">
            <endpoint address="" behaviorConfiguration="WebApplication1.Service1AspNetAjaxBehavior"
              binding="webHttpBinding" contract="WebApplication1.Service1" bindingConfiguration="mybinding"/>
          </service>
        </services>
      </system.serviceModel>



    WebForm1.aspx

    <script src="Scripts/jquery-3.3.1.js"></script>
        <script>
            $(function () {
                $.ajax({
                    type: "POST",
                    url: "/Service1.svc/Test",
                    contentType:"application/json",
                    data: '{ "Id": "Anna"}',
                    dataType:"json",
                    success: function (data) {
                        console.log(data.d);
                    }
                })
            })
        </script>

    IIS

    https://i.stack.imgur.com/WL7Ye.png

    Result.

    https://i.stack.imgur.com/Kxp9I.png

    Due to the self-signed certificate, the connection is not secure.
    There are some different configurations between us, such as you use the basichttpbining, and I don’t know how do you host the web applicaton, wish this example is useful to you.
    Best Regards
    Abraham

    Tuesday, October 30, 2018 2:32 AM
    Moderator