Getting SOAP Errors with ASMX Web Service e.g Unsupported Media Tyype

两盒软妹~` 提交于 2019-12-25 16:08:35

问题


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.

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://webservice.demo.co.uk

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://webservice.demo.co.uk/" />
  </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>

This is my console project app trying to run it

And with a C# consumer app run from console trying to get a response I get this error

C:\Users\rreid>"C:\Users\rreid\Documents\Visual Studio 2010\Projects\WebS
rvice\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://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body>
GetName xmlns="http://tempuri.org/"><ClientHash>6D104928-7633-4998-90C5-C01ABD4010B</ClientHash></GetName></soap12:Body></soap12:Envelope>
IN HTTPRequest POST - http://webservice.demo.co.uk/WebService.asmxop=GetName - <?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="
ttp://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><Getame xmlns="http://tempuri.org/"><ClientHash>6D104928-7633-4998-90C5-C01ABD4E010</ClientHash></GetName></soap12:Body></soap12:Envelope> - RobsBOT
End of Init
IN MakeHTTPRequest
IN AccessURL
MAKE POST REQUEST TO http://webservice.demo.co.uk/WebService.asmx?p=GetName
Make a POST request to http://webservice.demo.co.uk/WebService.asm?op=GetName
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://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://ww.w3.org/2001/XMLSchema" mlns:soap12="http://www.w3.org/2003/05/soap-envelope><soap12:Body><GetName xmlns="http://tempuri.org/"><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

These are the SOAP Request examples which come from hitting the debug button in VS 2010 and it running on local host on a port) - works fine e.g

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://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetName xmlns="http://tempuri.org/">
      <ClientHash>string</ClientHash>
    </GetName>
  </soap12:Body>
</soap12:Envelope>

So I have just replaced the values for host and so on with my URL on my server where the web service is based

These are my ASP attempts

Soap 1.1 example ASP Classic

POST /WebService.asmx HTTP/1.1
Host: webservice.demo.co.uk
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://webservice.demo.co.uk/WebService.asmx/GetName"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetName xmlns="http://webservice.demo.co.uk/WebService.asmx/">
      <ClientHash>string</ClientHash>
    </GetName>
  </soap:Body>
</soap:Envelope>

debug

IN SOAPPost & http://webservice.demo.co.uk/WebService.asmx
We will be posting to http://webservice.demo.co.uk/WebService.asmx
Build the SOAP Wrapper
our soap request is = <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap:Body> <GetName xmlns="http://webservice.demo.co.uk/"> <ClientHash>6D104928-7633-4998-90C5-C01ABD4E010B</ClientHash> </GetName> </soap:Body> </soap:Envelope>
IN MakeSOAPRequest
Make POST Request to http://webservice.demo.co.uk/WebService.asmx
Set Host Header to webservice.demo.co.uk
Set Content-Type Header to text/xml; charset=utf-8
Set SOAPAction Header to **/WebService.asmx/GetName**
Set user-agent to PDR Internal Crawler
Set Request Header Content-Length to 385
POST Request == <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap:Body> <GetName xmlns="http://webservice.demo.co.uk/"> <ClientHash>6D104928-7633-4998-90C5-C01ABD4E010B</ClientHash> </GetName> </soap:Body> </soap:Envelope>
No Error sending request so get response back
RESPONSE == <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>**Server did not recognize the value of HTTP Header SOAPAction: /GetName.**</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>

Server did not recognize the value of HTTP Header SOAPAction: /GetName.

Soap 1.2 example ASP Classic

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://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetName xmlns="http://tempuri.org/">
      <ClientHash>string</ClientHash>
    </GetName>
  </soap12:Body>
</soap12:Envelope>


IN SOAPPost & http://webservice.demo.co.uk/WebService.asmx
We will be posting to http://webservice.demo.co.uk/WebService.asmx
Build the SOAP Wrapper
our soap request is = <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetName xmlns="http://webservice.demo.co.uk/"> <ClientHash>6D104928-7633-4998-90C5-C01ABD4E010B</ClientHash> </GetName> </soap12:Body> </soap12:Envelope>
IN MakeSOAPRequest
Make POST Request to http://webservice.demo.co.uk/WebService.asmx
Set Host Header to webservice.demo.co.uk
Set Content-Type Header to application/soap+xml; charset=utf-8
Set user-agent to PDR Internal Crawler
Set Request Header Content-Length to 393
POST Request == <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetName xmlns="http://webservice.demo.co.uk/"> <ClientHash>6D104928-7633-4998-90C5-C01ABD4E010B</ClientHash> </GetName> </soap12:Body> </soap12:Envelope>
No Error sending request so get response back
RESPONSE == <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">**Server was unable to process request. ---&gt; Root element is missing.**</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

So Root Element missing? What? 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

来源:https://stackoverflow.com/questions/26253677/getting-soap-errors-with-asmx-web-service-e-g-unsupported-media-tyype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!