C# 400向WCF发送XML时请求错误
我试图将一些XML发布到web服务,每次发布时,都会返回400个错误请求 在MyService.cs中:C# 400向WCF发送XML时请求错误,c#,xml,wcf,C#,Xml,Wcf,我试图将一些XML发布到web服务,每次发布时,都会返回400个错误请求 在MyService.cs中: [ServiceContract(Namespace = "http://foo")] public class MyService { [OperationContract, WebInvoke(UriTemplate = "/GetData/", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageF
[ServiceContract(Namespace = "http://foo")]
public class MyService
{
[OperationContract, WebInvoke(UriTemplate = "/GetData/", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
public string GetData(DataRequest req)
{
return "Success!";
}
}
[DataContract(Namespace = "http://foo")]
public class DataRequest
{
[DataMember]
public string ID { get; set; }
[DataMember]
public string Data { get; set; }
}
在单独的控制台应用程序Program.cs中:
Path p = "C:\\Users\\sflan\\Desktop\\test.xml";
string url = "http://foo/MyService.svc/GetData/";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/xml";
byte[] data = Encoding.UTF8.GetBytes(p.readFile());
req.ContentLength = data.Length;
using (Stream s = req.GetRequestStream())
{
s.Write(data, 0, data.Length);
s.Close();
}
try
{
WebResponse response = req.GetResponse();
using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(rdr.ReadToEnd());
rdr.Close();
}
}
catch (Exception ex) { Console.WriteLine(ex.Message); }
我试图在文件中发送的XML:
<root>
<Users ID="2" Data="This is some sample data." />
</root>
我已经搜索了几个小时寻找解决方案,但一直找不到。我的web.config允许最大缓冲区大小/池大小和我能找到的所有相关内容,但仍然没有运气。值得注意的是,如果我从GetData方法签名中删除“DataRequestReq”,我会得到成功消息,但我需要能够处理XML数据
有人能告诉我我做错了什么吗?XML格式不好,我没有从围绕同一问题的帖子中了解到这一点 适用于此的适当XML如下所示:
<DataRequest xmlns="http://foo">
<Data>This is some sample data.</Data>
<ID>2</ID>
</DataRequest>
这是一些示例数据。
2.