我遇到Tcp连接请求问题。我有两个应用程序,一个是Asp.net网站,另一个是Asp.net WebApi项目。 Api用于公开Web用于移动应用程序的相同功能。我的问题是,有一种方法通过tcp连接请求pdf文件并返回pdf响应。在测试Web应用程序时,我得到了正确的字节响应,但是在使用Api进行测试时,我没有收到所有字节。造成这种变化的原因是什么?。
“RequestData是一个xml字符串,其中包含用于在另一侧生成pdf的参数”
这是tcp请求的代码的和平。
public static string Connect(string requestData)
{
// Create all required object and variables
TcpClient tcpConnect = new TcpClient();
NetworkStream tcpStream = null;
string responseData = string.Empty;
int bytesRead = 0;
int resLength = 0;
using (tcpConnect)
{
try
{
tcpConnect.Connect(hostAddress, hostPort);
}
catch (Exception ex)
{
throw ex;
}
// Open TCP stream and send data
using (tcpStream = tcpConnect.GetStream())
{
byte[] reqBuffer = Encoding.GetEncoding("ISO8859-1").GetBytes(requestData);
// Get the Big Endian representation of the length as a byte array.
byte[] lenBuffer = new byte[4];
lenBuffer[0] = (byte)((reqBuffer.Length & 0xFF000000) >> 24);
lenBuffer[1] = (byte)((reqBuffer.Length & 0x00FF0000) >> 16);
lenBuffer[2] = (byte)((reqBuffer.Length & 0x0000FF00) >> 8);
lenBuffer[3] = (byte)(reqBuffer.Length & 0x000000FF);
// Send length of request as Big Endian byte array
tcpStream.Write(lenBuffer, 0, lenBuffer.Length);
// Send request data as string
tcpStream.Write(reqBuffer, 0, reqBuffer.Length);
// Read 4 Bytes from the buffer, unless the socket is closed
bytesRead = 0;
bytesRead = tcpStream.Read(lenBuffer, bytesRead, lenBuffer.Length);
// If the number of bytes returned is 0 then throw an exception
if (bytesRead != 4)
{
throw new Exception("Unable to communicate with server or invalid data received");
}
// Get the length of the response byte array from a Big Endian representation.
resLength = (lenBuffer[0] << 24) + (lenBuffer[1] << 16) +
(lenBuffer[2] << 8) + lenBuffer[3];
// Keep reading from the socket into the buffer until we have the specified number of bytes.
byte[] resBuffer = new byte[resLength];
// Read 4 Bytes from the buffer, unless the socket is closed
bytesRead = 0;
// Issue is here on this below while loop. the web app will receive more than 30 000 bytesread and APi get 3 000 and this result to an api receiving a blank pdf
while (bytesRead < resBuffer.Length)
{
bytesRead += tcpStream.Read(resBuffer, bytesRead, resBuffer.Length - bytesRead);
if (bytesRead == 0)
{
break;
}
}
// If the number of bytes returned is 0 then throw an exception
if (bytesRead != resLength)
{
throw new Exception("Unable to communicate with server or invalid data received");
}
// Convert the response to valid string
responseData = Encoding.GetEncoding("ISO-8859-1").GetString(resBuffer);
// Close the stream
tcpStream.Close();
}
// Close the connection
tcpConnect.Close();
}
// Dispose of all objects
tcpConnect = null;
tcpStream = null;`enter code here`
// Return result data
return responseData;
}
答案 0 :(得分:0)
这根本不起作用,因为bytesRead只是第一次为0。因此,我不确定它如何与您的Asp网站一起使用,除非它是不同的代码。
bytesRead += tcpStream.Read(resBuffer, bytesRead, resBuffer.Length - bytesRead);
if (bytesRead == 0)
{
break;
}
你应该做的是:
int bRead = tcpStream.Read(resBuffer, bytesRead, resBuffer.Length - bytesRead);
if (bRead == 0)
{
break;
}
bytesRead += bRead;