How can I get HTML from the following web request?

2
string html = "";

using (WebClient webClient = new WebClient())
{
    string address = "https://#.tk/index-post.php";
    byte[] postData = Encoding.ASCII.GetBytes("name=wololo");
    webClient.Headers[HttpRequestHeader.Accept] = "text/html, application/xhtml+xml, */*";
    webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
    webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    webClient.UploadData(address, postData);
}

How can I get the HTML code from the page when it makes the request POST ?

    
asked by Xabat Gurtubai 23.10.2016 в 23:11
source

2 answers

1

The documentation of the method already gives you a clue


The method returns a byte[] with the server's response, and if you already know it's an HTML, you can transform it into a string:

byte[] responseArray = myWebClient.UploadData(uriString,postArray);

string htmlString = Encoding.ASCII.GetString(responseArray);
    
answered by 26.10.2016 в 18:23
0

I think there's a similar question here, but in the stackoverflow in English, here's the link , I hope it serves you

Then if you want to read the specific html here I leave you a method:

string html = "";
using( StreamReader reader = new StreamReader( @"c:\index.html" ) )
{
 String line = String.Emtpy;
 while( (line = reader.ReadLine()) != null )
 {
    html += line;
 }
}

Here is the link from where that example appears

    
answered by 25.10.2016 в 18:19