-2

Hi i Have created WCF service , Service contract and Data contract is pasted below

[ServiceContract]
public interface IRestWithXML
{
    [OperationContract]
    [WebInvoke(Method = "Post", UriTemplate = "DoWork", RequestFormat=                WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string DoWork(Test objtest);

    [OperationContract]
    [WebInvoke(Method = "Post", UriTemplate = "Method?test={strtest}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string Method(Test objtest,string strtest);

}          

[DataContract]
public class Test
{
    [DataMember]
    public string id { get; set; }
}

How should i test these services in .NET . I can able to test the methods by changing method "Post" to "GET" .

But I have to test these services using "Post" . Please guide me

thanks in advance !!!!

Parthi
  • 69
  • 1
  • 2
  • 12

3 Answers3

0

Just set the Method to "POST". http://msdn.microsoft.com/en-US/library/system.net.httpwebrequest.method.aspx

var myWebRequest = new HTTPWebRequest();
myWebRequest.Method = "POST"
TGlatzer
  • 5,815
  • 2
  • 25
  • 46
0

To test a POST request, you have to change some things in your code :

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "DoWork", RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string DoWork(Stream objtest);

You set the parameter to Stream.

In the implementation of DoWork(Stream objtest) :

public string DoWork(Stream objtest)
{
     StreamReader sr = new StreamReader(stream);
     string s = sr.ReadToEnd();
     sr.Dispose();
     NameValueCollection collection = System.Web.HttpUtility.ParseQueryString(s);
     return collection.ToString();
}

To test you request, you have to use a REST Client (Fiddler ?) and the content of you body will be in the collection.

If you want to create the request in C#:

string body ="&key1=value1&key2=value2";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write(body);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Joffrey Kern
  • 6,449
  • 3
  • 25
  • 25
  • Thanks for the reply ..I have tried but I'm getting Remote Server returned error .Method Not Allowed Error – Parthi Mar 08 '13 at 12:14