开发者

When I pass xml string as a request i get an Bad Request exception in WCF REST?

Client:

string value = "<?xml version=1.0?><person><fi开发者_如何学Crstname>john</firstname><lastname>smith</lastname> <person/>";

using (HttpResponseMessage response = m_RestHttpClient.Post("new/customerxml/"+value2, frm.CreateHttpContent()))
{

}

Server:

[OperationContract]
[WebInvoke(UriTemplate = "new/customerxml/string={value}", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
public string NewCustomer(string value)
{
        return value;
}


That's because WCF sniffs the content and decides you are uploading XML, not a string. Assuming you are using the HttpClient from the WCF REST Starter kit, try this:

    string value = "<?xml version=1.0?><person><firstname>john</firstname><lastname>smith</lastname> <person/>";

var content = HttpContentExtentions.CreateDataContract(value, typeof(string));
using (HttpResponseMessage response = m_RestHttpClient.Post("new/customerxml/"+value2, content)
    {
      ...
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜