使用ASMX Web服务获取SOAP错误,例如不支持的媒体类型

时间:2014-10-08 09:37:43

标签: c# xml web-services soap asmx

我已经获得了一个旧的web服务.asmx来使用,虽然代码在调试模式下工作正常(VS在本地主机上播放调试)当我尝试编写一个消费者应用程序来传递相关的SOAP请求以获得响应时我无法让它发挥作用。

在.NET中,我得到状态415不支持的媒体类型(之前从未听说过) - 科技问我发送了什么媒体我说文本是XML。

以下是所有代码和调试。我甚至尝试编写ASP经典版本,但根据SOAP版本得到不同的错误。

Web服务在一台服务器上。我正试图从我的本地机器和另一台服务器上消费。

WebMethods都在.amsx文件中,例如

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

在调试模式下从VS在localhost上运行它可以解决问题。将文件复制到IIS设置中的网站,名为http://webservice.demo.co.uk

其中包含以下文件夹结构

  

webservice.demo.co.uk/http.demo/App_Data(空)   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

配置文件内容

<?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>

这是我试图运行它的控制台项目应用程序

使用从控制台运行的C#消费者应用程序试图获得响应我得到了这个错误

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

这些是SOAP请求示例,它们来自VS 2010中的调试按钮,它在端口上的本地主机上运行) - 工作正常,例如

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>

所以我刚刚用我的服务器上的URL替换了host等的值,等等

这些是我的ASP尝试

Soap 1.1示例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>

调试

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>

所以根元素丢失了吗?什么? 并且Server无法识别HTTP Header SOAPAction的值:/ GetName。

那么我做错了什么?

我知道.asmx已经过时但我接管了这个项目。

我只需要一个消费者应用程序,可以在本地主机上从VS到服务器或服务器到服务器,显然可以使用VS.

为什么会有这么多错误

什么是不支持的媒体类型错误415?

我该如何解决?

0 个答案:

没有答案