0% found this document useful (0 votes)
5 views

Some Code

The document shows code for making a HTTP request to a website, decompressing the response if compressed, and loading the HTML into an HTML document object. It creates an HTTP client, makes a GET request, decompresses the response if needed, loads the HTML into an object and writes hello world to the console.

Uploaded by

Isaac William
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Some Code

The document shows code for making a HTTP request to a website, decompressing the response if compressed, and loading the HTML into an HTML document object. It creates an HTTP client, makes a GET request, decompresses the response if needed, loads the HTML into an object and writes hello world to the console.

Uploaded by

Isaac William
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using HtmlAgilityPack;

using System;
using System.IO;
using System.IO.Compression;
using System.Net.Cache;
using System.Net.Http;

namespace PriceChecker_
{
class Program
{
static void Main(string[] args)
{
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://brickseek.com/");
httpClient.DefaultRequestHeaders.Add(@"Accept", "text/html,
application/xhtml+xml");
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");
httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-
US,en;q=0.9");
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0
(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/88.0.4324.146 Safari/537.36");
var result = httpClient.GetAsync("url of item");

var response = result.Result;


var compdata = response.Content.ReadAsByteArrayAsync().Result;
var databytes = Decompress(compdata);
var data = System.Text.Encoding.Default.GetString(databytes);

HtmlDocument pageDocument = new HtmlDocument();


pageDocument.LoadHtml(data);

Console.WriteLine("Hello World!");
}

static byte[] Decompress(byte[] gzip)


{
// Create a GZIP stream with decompression mode.
// ... Then create a buffer and write into while reading from the
GZIP stream.
using (GZipStream stream = new GZipStream(new MemoryStream(gzip),
CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}

}
}

You might also like