使用ASMX Web服务的不支持的媒体类型

时间:2014-10-07 21:41:55

标签: c# xml web-services soap asmx

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

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

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

Web服务在一台服务器上。我试图从我的本地机器和另一台服务器消耗到该服务,以证明我可以像调试模式一样将XML传送回来。

WebMethods都在.amsx文件中,例如

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

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

调试和发布中相同的Config位于

之下

其中包含以下文件夹结构

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

配置文件内容

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

从debug运行它的例子是

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://webservice.demo/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/">
          <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://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <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://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/">
          <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://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>
        <GetNameResponse xmlns="http://webservice.demo/">
          <GetNameResult>string</GetNameResult>
        </GetNameResponse>
      </soap12:Body>
    </soap12:Envelope>

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

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://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://webservice.demo/">
            <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="http://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://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://webservice.demo.co.uk/WebService.asmx?op=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://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

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

我是否需要在请求或响应中添加额外的标头或以不同方式格式化XML?

我也试过从ASP Classic运行SOAP 1.1和1.2,但又找到了2个不同的错误:

Server was unable to process request. ---> Root element is missing.

And

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

那么我做错了什么?

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

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

为什么会有这么多错误

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

如何修复它以及我无法解决HTTP状态代码415的问题,因为阅读有数百种不同的可能性。

0 个答案:

没有答案