Reputation: 238
I'm trying to post some XML to a web service and every time I do, I get a 400 Bad Request error back.
In MyService.cs:
[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; }
}
In a separate console application 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); }
The XML I'm trying to send in the file:
<root>
<Users ID="2" Data="This is some sample data." />
</root>
I've searched around for hours looking for a solution, but have been unable to find one. My web.config allows max buffer size / poolsize and everything relevant I've been able to find, but still no luck. It's worth noting that if I remove "DataRequest req" from my GetData method signature, I get my success message, but I need to be able to work with the XML data.
Can anyone tell me what I'm doing wrong?
Upvotes: 2
Views: 693
Reputation: 238
The XML was poorly formatted, something I wasn't picking up from posts centered on the same issue.
The proper XML for this is as follows:
<DataRequest xmlns="http://foo">
<Data>This is some sample data.</Data>
<ID>2</ID>
</DataRequest>
Upvotes: 1