简体   繁体   English

如何在C#中编码tcpclient流?

[英]How to encoding tcpclient stream in c#?

I have a problem with my pop3 client. 我的pop3客户端有问题。 i can't encoding in utf-8 or iso 8859-1 the emails that i retrieve from the tcpclient stream. 我无法在utf-8或iso 8859-1中对我从tcpclient流中检索到的电子邮件进行编码。 This is the code: 这是代码:

TcpClient tcpClient = new TcpClient();
txtLog.Text = "Dico:\r\nConnect me to " + txtServer.Text + ":" + txtPort.Text + "\r\n\r\n";
tcpClient.Connect(txtServer.Text, Convert.ToInt32(txtPort.Text));
NetworkStream netStream = tcpClient.GetStream();
System.Text.Encoding iso_8859_1 = System.Text.Encoding.GetEncoding("iso-8859-1");
System.IO.StreamReader strReader = new System.IO.StreamReader(netStream, iso_8859_1);

System.Text.Encoding utf_8 = System.Text.Encoding.UTF8;


if (tcpClient.Connected)
{
    txtLog.Text = "Il server risponde:\r\n" + strReader.ReadLine() + "\r\n\r\n";
    byte[] WriteBuffer = new byte[100990024];
    ASCIIEncoding enc = new System.Text.ASCIIEncoding();

    WriteBuffer = enc.GetBytes("USER " + txtUser.Text + "\r\n");
    txtLog.Text = "Dico:\r\nHere's the username: " + txtUser.Text + "\r\n\r\n";
    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
    txtLog.Text = "Il server risponde\r\n" + strReader.ReadLine() + "\r\n\r\n";
    WriteBuffer = enc.GetBytes("PASS " + txtPass.Text + "\r\n");
    txtLog.Text = "Dico:\r\nHere's the password: " + txtPass.Text + "\r\n\r\n";
    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
    txtLog.Text = "Il server risponde\r\n" + strReader.ReadLine() + "\r\n\r\n";

    WriteBuffer = enc.GetBytes("RETR " + messaggio.Text + "\r\n");
    txtLog.Text = "Messaggio: " + messaggio.Text + "\r\n\r\n";
    string message;
    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
    while (true)
    {
        message = strReader.ReadLine();
        if (message == ".")
        {
            if (message == "/n") break;
            break;
        }
        else
        {
            txtLog.Text += message + "\r\n\r\n";
            continue;
        }
    }

I always get something like this "questa_=E8_una_prova_=F2_=EC_?=". 我总是收到类似“ questa_ = E8_una_prova_ = F2_ = EC _?=”的内容。 What's wrong? 怎么了? Thank you. 谢谢。

You're looking at the transfer encoding, which is used to transfer the bytes that result from the text encoding. 您正在查看传输编码,该传输编码用于传输文本编码产生的字节。

While the text may be encoded with UTF-8 or any other character set, the transfer encoding is usually base 64 or quoted-printable . 尽管可以使用UTF-8或任何其他字符集对文本进行编码,但是传输编码通常为base 64quoted-printable The string "questa_=E8_una_prova_=F2_=EC_?=" looks like the latter. 字符串"questa_=E8_una_prova_=F2_=EC_?="看起来像后者。

Take a look here on how to decode quoted-printable into a string, and see here for already existing POP3 client implementations in .NET. 在这里了解如何将quoted-printable解码为字符串,并在此处了解.NET中已经存在的POP3客户端实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM