服务器和.NET Compact Framework客户端上的ServiceStack

时间:2012-11-28 14:23:44

标签: rest compact-framework servicestack

我使用ServiceStack创建了我的服务器和客户端(MonoDroid和Windows),一切运行良好,但现在我需要使用Compact Framework F3.5的移动客户端从服务器中获取数据。

我可以将其作为SOAP webservice访问,但我更喜欢使用REST,并使用框架来简化操作,就像ServiceStack客户端一样(因为RestSharp与Compact Framework不兼容)。

您是否了解与CF3.5兼容的内容,可让我以简单的方式与ServiceStack服务器连接

var client = new JsonServiceClient("http://192.168.0.87:82");
HelloResponse response = client.Get(new Hello { Name = "World!" });

更新:

我设法通过HTTPWebRequest获取响应,并使用支持.NET Compact Framework的旧JSON.Net版本反序列化我的HelloResponse对象。 我唯一缺少的是如何序列化我假设的HelloReq对象并将其传递给HttpWebRequest,任何提示? (无需手动创建如下所示的路线)

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://192.168.0.87:82/Hello/test?format=json");
req.Method = "GET";           
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream respStream = resp.GetResponseStream();
string resps;

using (var reader = new StreamReader(respStream, Encoding.UTF8))
{
      resps = reader.ReadToEnd();
}
respStream.Close();

JsonTextReader jreader = new JsonTextReader(new StringReader(resps));

JsonSerializer serializer = new JsonSerializer();
HelloResponse p = serializer.Deserialize<HelloResponse>(jreader);

谢谢!

1 个答案:

答案 0 :(得分:0)

创建请求:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://192.168.0.87:82/json/syncreply/Hello");
            req.ContentType = "text/json";
            req.Method = "POST";
            req.ContentLength = json.Length;

            using (var streamWriter = new StreamWriter(req.GetRequestStream()))
            {
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }