Netty消息被切断了

时间:2016-07-26 07:28:05

标签: java c# netty

我有一个java http服务器,它正在使用Netty 4.0.39

当我在HttpResponse中发送一个大字符串时,C#Client只接收四分之一的发送数据。

在java中发送数据的方法是

    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
    response.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/octet-stream; charset=UTF-8");
    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, finalEncoded.length());
    response.content().writeBytes(Unpooled.copiedBuffer(finalEncoded, CharsetUtil.UTF_8));
    channel.writeAndFlush(response);

我正在谈论的大小高达1000000字节。 大多数情况下,消息始终在同一点被切断

修改

用于阅读消息的C#代码

private byte[] makeRequest(byte[] data, int timeout = 0) 
{ 
     WebRequest req = WebRequest.Create(uri); 
     req.Method = "POST"; 
     ((HttpWebRequest)req).ProtocolVersion = HttpVersion.Version11; 
     if (UseProxy && webProxy != null) 
     { 
        req.Proxy = webProxy; 
        req.PreAuthenticate = false; 
        //req.UseDefaultCredentials = false; 
     } 
     req.ContentLength = data.Length; 
     if (timeout > 0) 
     { 
        req.Timeout = timeout; 
     } 
     using (var stream = req.GetRequestStream()) 
     { 
        stream.Write(data, 0, data.Length); 
        stream.Close(); 
     } 
     lock (requestsLock) 
     { 
        openWebRequests.Add(req); 
     } 
     byte[] responseBytes = null; 
     try 
     { 
        using (var response = req.GetResponse()) 
        { 
            if (response == null) return null; 
            responseBytes = ReadFully(response.GetResponseStream()); 
        } 
      } 
      catch (Exception e) 
      { 
        Log(MessagingTypes.ProtocolLevels.Exceptions, e.Message); 
        return null; 
      } 
      return responseBytes; 
} 


private byte[] ReadFully(Stream input)
{
     using (MemoryStream ms = new MemoryStream())
     {
             input.CopyTo(ms);
             return ms.ToArray();
     }
}

1 个答案:

答案 0 :(得分:0)

我们最终将包发送为HttpChunks。之后一切正常。