C#实现EDI请求的示例代码,按照FFTIN格式实现EDI请求交互,数据内容长度分部为2,1,3,10,35,35。
时间: 2023-08-04 21:03:16 浏览: 160
下面是一个简单的C#示例代码,用于按照FFTIN格式实现EDI请求交互:
```csharp
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
public class EdiRequest
{
public static void Main()
{
string host = "example.com";
int port = 1234;
try
{
// Connect to the server
TcpClient client = new TcpClient(host, port);
// Get the network stream
NetworkStream stream = client.GetStream();
// Construct the data to be sent
string data = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string[] lengths = { "02", "01", "03", "10", "35", "35" };
StringBuilder request = new StringBuilder("FFTIN");
for (int i = 0; i < lengths.Length; i++)
{
request.Append(lengths[i]);
request.Append(data.Substring(0, int.Parse(lengths[i])));
data = data.Substring(int.Parse(lengths[i]));
}
// Convert the request string to bytes and send it
byte[] bytes = Encoding.ASCII.GetBytes(request.ToString());
stream.Write(bytes, 0, bytes.Length);
// Receive the response from the server
bytes = new byte[client.ReceiveBufferSize];
int bytesRead = stream.Read(bytes, 0, client.ReceiveBufferSize);
string response = Encoding.ASCII.GetString(bytes, 0, bytesRead);
Console.WriteLine("Response received: {0}", response);
// Close the connection
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e);
}
}
}
```
这段代码假设您已经知道要连接的服务器的主机名和端口号,并且数据内容已经准备好。它使用TCP客户端连接到服务器,并将EDI请求发送到服务器。在请求的构造过程中,使用了字符串长度和数据内容,这些数据按照FFTIN格式分别为2、1、3、10、35和35个字符。请求发送后,代码从服务器接收响应并在控制台上显示它。最后,客户端关闭连接。
阅读全文