First of all, you need to know the exact URL of your REST service. Since you specified http://localhost:2517/Service1/Create
, just try opening the same URL from IE, and you should get a method that is not allowed, since your Create
method is specific to WebInvoke and IE is executing WebGet.
Now, make sure you have a SampleItem in your client application defined in the same namespace on your server, or make sure that the xml string you are building has an appropriate namespace for the service to identify that the xml string of the sample object can deserialize back to the object on the server.
I have a SampleItem defined on my server as shown below:
namespace SampleApp { public class SampleItem { public int Id { get; set; } public string StringValue { get; set; } } }
The xml line corresponding to my SampleItem looks like this:
<SampleItem xmlns="http://schemas.datacontract.org/2004/07/SampleApp" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Id>6</Id><StringValue>from client testing</StringValue></SampleItem>
Now I use the method below to do a POST for the REST service:
private string UseHttpWebApproach<T>(string serviceUrl, string resourceUrl, string method, T requestBody) { string responseMessage = null; var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest; if (request != null) { request.ContentType = "application/xml"; request.Method = method; } //var objContent = HttpContentExtensions.CreateDataContract(requestBody); if(method == "POST" && requestBody != null) { byte[] requestBodyBytes = ToByteArrayUsingDataContractSer(requestBody); request.ContentLength = requestBodyBytes.Length; using (Stream postStream = request.GetRequestStream()) postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length); } if (request != null) { var response = request.GetResponse() as HttpWebResponse; if(response.StatusCode == HttpStatusCode.OK) { Stream responseStream = response.GetResponseStream(); if (responseStream != null) { var reader = new StreamReader(responseStream); responseMessage = reader.ReadToEnd(); } } else { responseMessage = response.StatusDescription; } } return responseMessage; } private static byte[] ToByteArrayUsingDataContractSer<T>(T requestBody) { byte[] bytes = null; var serializer1 = new DataContractSerializer(typeof(T)); var ms1 = new MemoryStream(); serializer1.WriteObject(ms1, requestBody); ms1.Position = 0; var reader = new StreamReader(ms1); bytes = ms1.ToArray(); return bytes; }
Now I call the above method as shown:
SampleItem objSample = new SampleItem(); objSample.Id = 7; objSample.StringValue = "from client testing"; string serviceBaseUrl = "http://localhost:2517/Service1"; string resourceUrl = "/Create"; string method="POST"; UseHttpWebApproach<SampleItem>(serviceBaseUrl, resourceUrl, method, objSample);
I have a SampleItem object defined on the client side. If you want to build an xml string on the client and pass, you can use the method below:
private string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string xmlRequestBody) { string responseMessage = null; var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest; if (request != null) { request.ContentType = "application/xml"; request.Method = method; } //var objContent = HttpContentExtensions.CreateDataContract(requestBody); if(method == "POST" && requestBody != null) { byte[] requestBodyBytes = ASCIIEncoding.UTF8.GetBytes(xmlRequestBody.ToString()); request.ContentLength = requestBodyBytes.Length; using (Stream postStream = request.GetRequestStream()) postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length); } if (request != null) { var response = request.GetResponse() as HttpWebResponse; if(response.StatusCode == HttpStatusCode.OK) { Stream responseStream = response.GetResponseStream(); if (responseStream != null) { var reader = new StreamReader(responseStream); responseMessage = reader.ReadToEnd(); } } else { responseMessage = response.StatusDescription; } } return responseMessage; }
And the call to the above method will be as below:
string sample = "<SampleItem xmlns=\"http://schemas.datacontract.org/2004/07/XmlRestService\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Id>6</Id><StringValue>from client testing</StringValue></SampleItem>"; string serviceBaseUrl = "http://localhost:2517/Service1"; string resourceUrl = "/Create"; string method="POST"; UseHttpWebApproach<string>(serviceBaseUrl, resourceUrl, method, sample);
NOTE. Just make sure your URL is correct