2012-08-01 68 views
0

我试图与我的休息服务交流,但这一次总是返回我的发表参数是空的,但在我的客户端控制台中,他被填充。REST服务收到空参数

这里是接口类:

[ServiceContract] 
    public interface IMyTest 
    { 
     [OperationContract] 
     [WebInvoke(UriTemplate = "TestMe", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped)] 
     string TestMe(string parameter);   
    } 

我的SVC方法:

public string TestMe(string parameter) 
      { 
      if (string.IsNullOrEmpty(parameter) 
       return "Empty"; 
      return "OK"; 
      } 

我的客户:

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()); 

是我的客户端代码不好吗?我的配置不好吗? ...

谢谢。

+0

你是否也传递了内容长度标题? – 2012-08-01 16:38:14

+0

我也测试过,通过content-lentgh但没有任何改变。 – ahikaz 2012-08-02 09:57:17

回答

0

我创建了一个很好的tutorial,它帮助我用stream参数实现了方法。谢谢。

0

如果你想获得的请求字符串,使用流而不是字符串参数

public void TestMe(Stream dataStream) 

但如果没有,使用序列化到JSON对象作为参数。

+0

我测试将字符串更改为流,但现在我有响应400错误的请求。 – ahikaz 2012-08-02 09:38:20