.net web服务问题:请求格式无效:text / xml

时间:2016-10-10 02:18:26

标签: .net xml web-services

我正在使用.net框架编写一个简单的Web服务DEMO,并且我正在使用名为" soapUI"的工具测试Web服务。我试图通过HTTP HOST用XML数据发送请求消息,但我有一个问题。

  1. 我的网络方法的原型:
  2. [WebMethod]
    [WebInvoke(ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public XmlDocument loginrequest(string XmlString)
    {
        XmlDocument Xdoc = new XmlDocument();
        // parse request XML data, and return response XML document
        // ......
        return Xdoc;
    }
    
    1. 这是我的Web.config文件:
    2. <configuration>
          <system.web>
              <compilation targetFramework="4.5" debug="true"/>
              <httpRuntime targetFramework="4.5" requestValidationMode="2.0"/>
              <pages validateRequest="false"/>
              <webServices>
                  <protocols>
                     <add name="HttpGet"/>
                     <add name="HttpPost"/>
                  </protocols>
              </webServices>    
          </system.web>
      </configuration>
      
      1. 如果我按照以下方式发送HTTP POST数据,那么一切都正常工作(此处省略了HTTP标头以避免冗余,而ContentType为application/x-www-form-urlencoded)
      2. XmlString=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%3CLoginRequest%3E%3CUser%3E%3CUserName%3ETestUser%3C%2FUserName%3E%3CPassword%3E123456%3C%2FPassword%3E%3C%2FUser%3E%3CSeqNum%3E%3C%2FSeqNum%3E%3C%2FLoginRequest%3E
        
        1. 如果我发送HTTP POST数据,则收到错误消息(此格式正是我想要的,HTTP标头中的Content-Type为text/xmlapplication/xml):
        2. <?xml version="1.0" encoding="utf-8"?>
              <LoginRequest>
                  <User>
                      <UserName>TestUser</UserName>
                      <Password>123456</Password>
                  </User>
                  <SeqNum>
                  </SeqNum>
              </LoginRequest>
          

          以下是完整的错误消息:

          System.InvalidOperationException: Request format is invalid: application/xml.
             at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
             at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
          

          总结:由于soapUI似乎是一个广泛使用的测试工具,它应该没问题。我想知道的是,我是否需要更改我的Web服务代码或Web.config中的任何内容以支持XML格式的HTTP POST数据?

2 个答案:

答案 0 :(得分:0)

实际上我试图做的是发送XML字符串作为HTTP POST请求的实体主体,而不是查询字符串。我还不确定为什么会这样做

  

请求格式无效

但最后我通过HttpContext.Current.Request.InputStream阅读请求实体 - 解决了这个问题。

答案 1 :(得分:-1)

相关问题