Search code examples
c#webformshttpwebrequest

Using web request to fetch and show strings in client side


So with the below code I managed to fetch the strings that are in a 3rd party website and using table I am displaying it on the client side of my project. But the problem is that each letters are separated on all the strings as shown in the below picture.

enter image description here

The below would be the code I use,

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);
    }

How can I fix it? Thanks in advance.


Solution

  • If you receive list of strings, then you can use literal control:

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

    If you receive html in body, then you need to escape it:

    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 />"});
    }