开发者

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

开发者 https://www.devze.com 2023-01-11 22:32 出处:网络
Client: string value = \"<?xml version=1.0?><person><fi开发者_如何学Crstname>john</firstname><lastname>smith</lastname> <person/>\";

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

精彩评论

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

关注公众号