Unsupported Media Type with ASMX Web Service



I have been given an old webservice .asmx to work with and although the code works fine in debug mode (VS play debug on local host) when I try and write a consumer app to pass the relevant SOAP request to get a response I cannot get it to work.


In .NET I get Status 415 Unsupported Media Type (never heard of that before) - tech asked me what media I was sending I said text as XML.


Here is all the code plus debug. I tried even writing an ASP classic version but get different errors depending on the SOAP version.


The webservice is on one server. I am trying to consume from my local machine and another server to that service to prove I can get XML to it an back just like in debug mode.



WebMethods are all in the .amsx file e.g

[WebMethod]
public string GetName(string ClientHash)
{
}

Running it all on localhost from VS in debug mode works a treat. Copied the files to a website in IIS setup for me called http://ift.tt/1vQdAUw

The Config which is the same in debug and release is below

Has in it the following folder structure

webservice.demo.co.uk/http.demo/App_Data (empty)
webservice.demo.co.uk/http.demo/bin
-WebService.dll
-WebService.pdb
webservice.demo.co.uk/http.demo/properties
-AssemblyInfo
webservice.demo.co.uk/http.demo/
-WebService.asmx
-Web.Config


Config file contents


<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>

<connectionStrings>
<add name="APIDatabase" connectionString="xxx" />
<add name="Scotland" connectionString="xxx" />
</connectionStrings>
<appSettings>
<add key="SERVER" value="SERVER1" />
<add key="DEMOSERVER" value="SERVER12" />
<add key="ServiceURL" value="http://ift.tt/1vQdAUw" />
</appSettings>
<system.web>
<compilation debug="true" >
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<!--
The system.webServer section is required for running ASP.NET AJAX under Internet
Information Services 7.0. It is not necessary for previous version of IIS.
-->
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule" />
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory" />
<remove name="ScriptHandlerFactoryAppServices" />
<remove name="ScriptResource" />
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>


The examples when running it from debug is

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /WebService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://ift.tt/1uY7V24"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY" xmlns:soap="http://ift.tt/sVJIaE">
<soap:Body>
<GetName xmlns="http://webservice.demo/">
<ClientHash>string</ClientHash>
</GetName>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY" xmlns:soap="http://ift.tt/sVJIaE">
<soap:Body>
<GetNameResponse xmlns="http://webservice.demo/">
<GetNameResult>string</GetNameResult>
</GetNameResponse>
</soap:Body>
</soap:Envelope>


SOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

POST /WebService.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY" xmlns:soap12="http://ift.tt/18hkEkn">
<soap12:Body>
<GetName xmlns="http://webservice.demo/">
<ClientHash>string</ClientHash>
</GetName>
</soap12:Body>
</soap12:Envelope>

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY" xmlns:soap12="http://ift.tt/18hkEkn">
<soap12:Body>
<GetNameResponse xmlns="http://webservice.demo/">
<GetNameResult>string</GetNameResult>
</GetNameResponse>
</soap12:Body>
</soap12:Envelope>

This is my console project app debug trying to run it


C:\Users\rreid>"C:\Users\me\Documents\Visual Studio 2010\Projects\WebSrvice\TestWebService\bin\Debug\TestWebService.exe"
Start 07/10/2014 16:33:31
Send this XML ==

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/1uY7V28" xmlns:soap12="http://ift.tt/18hkEkn">
<soap12:Body>
<GetName xmlns="http://webservice.demo/">
<ClientHash>6D104928-7633-4998-90C5-C01ABD4010B</ClientHash>
</GetName>
</soap12:Body>
</soap12:Envelope>

IN HTTPRequest POST - http://ift.tt/1vQdBbf

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/1uY7V2i" xmlns:soap12="http://ift.tt/18hkEkn">
<soap12:Body>
<Getame xmlns="http://webservice.demo/">
<ClientHash>6D104928-7633-4998-90C5-C01ABD4E010</ClientHash>
</GetName>
</soap12:Body>
</soap12:Envelope>

Useragent - RobsBOT
End of Init
IN MakeHTTPRequest
IN AccessURL
MAKE POST REQUEST TO http://ift.tt/1vQdAnw
Make a POST request to http://ift.tt/1uY7Tat
Do we add headers
make request with RobsBOT
Convert post data into stream =

<?xml version="1.0" encoding="utf-8"?>
<soap12:Evelope xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/1vQdBrA" mlns:soap12="http://ift.tt/1vQdBrE;
<GetName xmlns="http://webservice.demo/">
<ClientHash>6D104928-7633-498-90C5-C01ABD4E010B</ClientHash>
</GetName>
</soap12:Body>
</soap12:Envelope>

**WebExeption The remote server returned an error: (415) Unsupported Media Type.
status = 415 - Unsupported Media Type
error = The remote server returned an error: (415) Unsupported Media Type.
ERROR = Unknown Error
StatusCode is 415 - Tries = 1**
Error is Unknown Error
Stop 07/10/2014 16:33:34

What are the HTTP Status 415 error Unsupported Media Type?

Do I need to add extra headers to the request or response or format the XML differently?

I also tried running both SOAP 1.1 and 1.2 from ASP Classic but got back 2 different errors

Server was unable to process request. ---> Root element is missing.
And Server did not recognize the value of HTTP Header SOAPAction: /GetName.

So what am I doing wrong?

I know .asmx is old but I have taken over this project.

I just need a consumer app that can take what obviously works from VS on localhost but from PC to Server or Server to Server.

Why so many errors

And what is the Unsupported Media Type Error 415?

How can I fix it and what cant I do to resolve HTTP Status Code 415 as from reading there are a million different possibilities.

No comments:

Post a Comment