.Net Web Service content-type
I need to create a .Net web service (WCF is out of the question) that should receive xml and return xml. I initially setup the function like so:
[WebMethod]
public string myFunc(string xmlRequest)
{
How can I change the HTTP POST content-type to text/xml? Also, I'm returning the xml response as a string, but in the web service help page it says the response will be:开发者_如何转开发
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://www.mysite.com/myFunc">string</string>
I need to remove that root <string>
element somehow.
This web service is called by another company who is probably not using .Net to post the XML as text/xml to my endpoint.
I have an open source web service framework (runs on .NET and Mono) that greatly simplifies the effort required in creating web services. It's automatically configured to provide XML(+REST), JSON(+REST), SOAP 1.1/1.2 endpoints for all your web services:
It's available from:
http://www.servicestack.net/
And there is a live javascript and silverlight demo available (running on CentOS/Nginx) at: http://www.servicestack.net/ServiceStack.Examples.Clients/Default.htm
And an online tutorial available that walks the process of creating and consuming web services (for MonoTouch but applicable everywhere) available here:
http://www.servicestack.net/monotouch/remote-info/
To remove the String, you'll have to make the return parameter xml like this:
public **System.Xml.XmlDocument** MyFunc(string xmlRequest)
This will make the response become;
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
xml
A valid XML document must contain a root element. It's not XML unless that string
element is there. Otherwise, it would be a plain text response.
Hang on, are you wanting to post your data as XML but receive it back as a string of XML? If I haven't misunderstood what you are saying, that is certainly what it sounds like you are doing, and that feels quite wrong, to be honest.
Can't you parse the string into an XML document in the web service, and what is the reason that the other company can't cope with an XML document with a single node? Can't they get the string value from that single node and then parse the string XML themselves?
Well the <string>
element shows up as it is, because your method is defined to return a string. Behind the scenes, .NET actually just takes whatever object is returned by your method, and serializes it. A serialized string is represented as <string>actual string contents</string>
.
What you want to do is either somehow return an object that serializes to exactly the message you want, or, assuming your string here actually contains the XML, write it directly to the response stream.
I've never actually tried that from a [WebMethod], but it might work something like this:
[WebMethod]
public void myFunc(string xmlRequest)
{
var myXml = @"<?xml version="1.0" encoding="utf-8"?><myRoot><myElement /></myRoot>";
Response.ContentType = "text/xml";
Response.Write(myXml);
Response.Flush();
Response.Close();
}
As a side note, WCF actually does the same thing. If you define a WCF service operation:
[OperationContract]
public string myFunc()
{
return "hi";
}
The returned XML (that is actually further wrapped in SOAP) is <string>hi</string>
Just like you have here with classic web services. Its a bit more of a pain to 'unwrap' that XML in WCF. you have to make a custom message writer, IIRC.
精彩评论