2

我编写了一个使用 servicestack 的服务器,以及一个使用 JSON 和 protobuf-net 连接到它的客户端(所以我确信服务器可以工作......)。现在我需要使用 CF3.5 在 Windows Mobile 上开发相同的客户端,并且由于 CF3.5 不支持 servicestack,因此我在客户端 HttpWebRequest 和 NewtonSoft.Json.Compact 上使用了 json 部分,这样:

课程:

[ProtoContract]
public class ReqPing 
{
}

[ProtoContract]
public class RespPing
{
    [ProtoMember(1)]
    public string Result { get; set; }
}

功能:

ReqPing iReqPing = new ReqPing();

string json = JsonConvert.SerializeObject(iReqPing);

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://192.168.0.87:82/json/syncreply/ReqPing");
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();
            }

            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();
            RespPing iRespPing = serializer.Deserialize<RespPing>(jreader);

它有效,所以现在我正在尝试用 protobuf 实现相同的功能,但我被困在这里:

ReqPing iReqPing = new ReqPing();


var ms = new MemoryStream();?????? correct way?

Serializer.Serialize<ReqPing>(ms, iReqPing);

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://192.168.0.87:82/x-protobuf/reply/ReqPing");
            req.ContentType = "application/x-protobuf";
            req.Method = "POST";
            req.ContentLength = ????????

how can I write the serialized stream to req??               

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            Stream respStream = resp.GetResponseStream();

            RespPing iRespPing = Serializer.Deserialize<RespPing>(respStream);

任何人都可以建议我什么是正确的方法?

谢谢!马蒂亚

编辑:

好的,关于如何使用流,但我也需要设置 ContentLength,否则我在 GetRequestStream 上收到错误消息(说我必须设置 ContentLength ...)。我使它与:

byte[] data;
        using(var stream = new MemoryStream()) 
        {
            Serializer.Serialize(stream, iReqPing);
            data = stream.ToArray();
            req.ContentLength = data.Length;
        }

        using(var stream = req.GetRequestStream()) 
        {
            stream.Write(data, 0, data.Length);                
        }

我还有另一种明显的方式吗?

另一个问题:我让它工作,但在两次请求之后,第三次它在 GetRequestStream 中超时。我注意到我在之前的调用中忘记了在我的 HttpWebResponse 中调用 Close(),我更正了它,现在可以工作了。奇怪的是,即使使用 JSON 版本,我也忘记关闭 HttpWebResponse,而且我没有遇到任何问题......例程完全一样......你能猜到原因吗?再次感谢!

4

1 回答 1

1

如何将序列化流写入 req?

using(var stream = req.GetRequestStream()) {
    Serializer.Serialize(stream, iReqPing);
}

然后反序列化,因为您已经正确:

RespPing iRespPing;
using(var stream = resp.GetResponseStream()) {
    iRespPing = Serializer.Deserialize<RespPing>(respStream);
}

不需要 a MemoryStream,除非您特别想手动发送长度。如果您确实想缓冲,那么也许:

byte[] data;
using(var stream = new MemoryStream()) {
    Serializer.Serialize(stream, iReqPing);
    data = stream.ToArray();
}
...
using(var stream = req.GetRequestStream()) {
    stream.Write(data, 0, data.Length);
}

编辑以显示更有效地使用MemoryStream重新现在编辑的问题:

    byte[] data;
    int len;
    using(var stream = new MemoryStream()) 
    {
        Serializer.Serialize(stream, iReqPing);
        data = stream.GetBuffer(); // note this is oversized!
        len = (int)stream.Length;
    }
    req.ContentLength = len;

    using(var stream = req.GetRequestStream()) 
    {
        stream.Write(data, 0, len); 
    }

这里的微妙之处在于我们避免了复制MemoryStream's 缓冲区——相反,我们获得了超大的后台缓冲区(记住要注意其中有多少实际上是数据),然后只写入那么多字节。所以data.Length可能是(例如)4096,但len可能是3012。这避免了额外的byte[]分配和Buffer.BlockCopy.

于 2013-04-25T06:29:24.273 回答