使用网络请求在客户端获取和显示字符串

Using web request to fetch and show strings in client side

因此,使用下面的代码,我设法获取了第 3 方网站中的字符串,并使用 table 在我的项目的客户端显示它。但问题是每个字母在所有字符串上都是分开的,如下图所示。

下面是我使用的代码,

public void Main()
    {
        WebRequest request = WebRequest.Create(
          "http://www.example.com");
        request.Credentials = CredentialCache.DefaultCredentials;
        WebResponse response = request.GetResponse();
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        var responseFromServer = reader.ReadToEnd();
        Console.WriteLine(responseFromServer);
        reader.Close();
        response.Close();
        var responseList = responseFromServer.Split('\n').ToList();
        var remaining = sqlList.Where(x => !responseList.Contains(x)).ToList();   
        var remaining1 = responseList.Where(x => !sqlList.Contains(x)).ToList();  
        var table = new Table();
        foreach(var row in remaining1)
        {
            var tableRow = new TableRow();
            foreach (var cell in row.Select(item => new TableCell { Text = item.ToString()}))
            {
                tableRow.Cells.Add(cell);
            }
            table.Rows.Add(tableRow);
        }
        Page.Controls.Add(table);
    }

我该如何解决?提前致谢。

如果你收到字符串列表,那么你可以使用文字控制:

var label = new Literal() { Mode = LiteralMode.PassThrough };
label.Text = string.Join("<br />", remaining1);                
Controls.Add(label);

如果你在正文中收到html,那么你需要转义它:

foreach (var line in remaining1)
{
     var label = new Literal() { Mode = LiteralMode.Encode };
     label.Text = line;                
     Controls.Add(label);
     Controls.Add(new Literal(){Mode = LiteralMode.PassThrough,Text = "<br />"});
}