问题描述:

I'm trying to comunicate with my rest service but this one return always that my sended parameter is empty but in my client console he is filled.

Here is the Interface Class :

[ServiceContract]

public interface IMyTest

{

[OperationContract]

[WebInvoke(UriTemplate = "TestMe", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped)]

string TestMe(string parameter);

}

My svc method :

 public string TestMe(string parameter)

{

if (string.IsNullOrEmpty(parameter)

return "Empty";

return "OK";

}

My client :

string content = "{\"Param\" : \"TEST\"}";

var request = WebRequest.Create("http://localhost/MyTestURL/MyTest.svc/TestMe");

request.Method = "POST";

request.ContentType = "application/json";

using (var writer = new StreamWriter(request.GetRequestStream()))

{

writer.Write(content);

}

var res = (WebResponse)request.GetResponse();

StreamReader reader =

new StreamReader(res.GetResponseStream(), Encoding.UTF8);

System.Console.WriteLine("Response");

System.Console.WriteLine(reader.ReadToEnd().ToString());

Is my client code not ok ? My conifiguration not ok ? ...

Thanks.

网友答案:

If you want to get request as string, use Stream instead of String parameter

public void TestMe(Stream dataStream)

but if not, use serializable to Json objects as parameters.

网友答案:

i founded a good tutorial that helped me to implement the method with the stream parameter. thanks.

相关阅读:
Top