简体   繁体   中英

.net web service issue: Request format is invalid: text/xml

I'm writing a simple web service DEMO using .net framework, and I'm testing the web service with a tool named "soapUI". I'm trying to send the request message by HTTP HOST with XML data but I have an issue.

  1. Protype of my web method:
[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. Here is my Web.config file:
<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. Everything is working just fine if I send HTTP POST Data as following (HTTP Header is omitted here to avoid redundancy, while the ContentType is application/x-www-form-urlencoded) :
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. I got an error message if I send HTTP POST Data as following (This format is exactly what I want, with Content-Type in HTTP header being text/xml or application/xml ):
<?xml version="1.0" encoding="utf-8"?>
    <LoginRequest>
        <User>
            <UserName>TestUser</UserName>
            <Password>123456</Password>
        </User>
        <SeqNum>
        </SeqNum>
    </LoginRequest>

Here is the full error message:

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

Summary: since the soapUI seems like to be a widely used test tool, it should be fine. What I want to know is that, do I need to change anything in my web service code or Web.config to support XML formatted HTTP POST data?

Actually what I was trying to do, is send a XML string as the entity-body of a HTTP POST request, instead of a query string. I'm still not sure why I got

Invalid request format

But finally I've solved the issue by reading the request entity-body through HttpContext.Current.Request.InputStream .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM