Downloads contents as a byte array or string, depending on need.
-
namespace VK.Snippets
-
{
-
using System.Collections.Generic;
-
using System.IO;
-
using System.Net;
-
using System.Text;
-
-
public static class HttpFetcher
-
{
-
public static string FetchPage(string url, string username = "", string password = "", string domain = "")
-
{
-
string page = Encoding.ASCII.GetString(FetchContent(url, username, password, domain));
-
-
return page;
-
}
-
-
public static byte[] FetchContent(string url, string username = "", string password = "", string domain = "")
-
{
-
List<byte> contentBytes = new List<byte>();
-
-
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
-
if (username != "" && password != "" && domain != "")
-
{
-
request.Credentials = new NetworkCredential(username, password, domain);
-
}
-
-
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
if (response.StatusCode == HttpStatusCode.OK)
-
{
-
Stream responseStream = response.GetResponseStream();
-
-
int b;
-
while ((b = responseStream.ReadByte()) != -1)
-
{
-
contentBytes.Add((byte)b);
-
}
-
}
-
-
return contentBytes.ToArray();
-
}
-
}
-
}