Utilizando o HttpWebRequest é possivel buscar o conteudo HTML de uma página.
Abaixo um exemplo:
public static String BuscarHtmlPagina(String url)
{
ASCIIEncoding encoding = new ASCIIEncoding();
String parametro = String.Empty;
byte[] buffer = encoding.GetBytes(parametro);
System.Uri enderecoRequisicaoTemplate = new Uri(url);
HttpWebRequest requisicaoWeb = (HttpWebRequest)WebRequest.Create(enderecoRequisicaoTemplate);
if (!String.IsNullOrEmpty(ConfigurationSettings.AppSettings["USUARIO-AUTENTICACAO"]))
{
requisicaoWeb.PreAuthenticate = true;
CredentialCache credencial = new CredentialCache();
credencial.Add(enderecoRequisicaoTemplate, "Basic", new NetworkCredential(ConfigurationSettings.AppSettings["USUARIO-AUTENTICACAO"], ConfigurationSettings.AppSettings["SENHA-AUTENTICACAO"], ConfigurationSettings.AppSettings["DOMINIO-AUTENTICACAO"]));
requisicaoWeb.Credentials = credencial;
}
if (!String.IsNullOrEmpty(ConfigurationSettings.AppSettings["NOME-PROXY"]))
{
System.Net.WebProxy proxy = new System.Net.WebProxy(ConfigurationSettings.AppSettings["NOME-PROXY"], Convert.ToInt32(ConfigurationSettings.AppSettings["PORTA-PROXY"]));
String usuarioProxy = ConfigurationSettings.AppSettings["USUARIO-PROXY"];
String senhaProxy = ConfigurationSettings.AppSettings["SENHA-PROXY"];
String dominioProxy = ConfigurationSettings.AppSettings["DOMINIO-PROXY"];
proxy.Credentials = new System.Net.NetworkCredential(usuarioProxy, senhaProxy, dominioProxy);
requisicaoWeb.Proxy = proxy;
}
requisicaoWeb.ContentType = "text/html; charset=windows-1252";
requisicaoWeb.Timeout = 60000;
requisicaoWeb.KeepAlive = true;
requisicaoWeb.UserAgent = "Mozilla/4.0 (compatible;MSIE 6.0b;Windows NT 5.0)";
requisicaoWeb.ContentLength = buffer.Length;
requisicaoWeb.Method = "POST";
requisicaoWeb.AllowAutoRedirect = true;
requisicaoWeb.AllowWriteStreamBuffering = true;
requisicaoWeb.MaximumAutomaticRedirections = 1;
requisicaoWeb.Method = "POST";
requisicaoWeb.ContentLength = buffer.Length;
Stream stream = requisicaoWeb.GetRequestStream();
stream.Write(buffer, 0, buffer.Length);
stream.Close();
HttpWebResponse httpWebResponse = null;
Stream resposta = null;
StreamReader streamRead = null;
Char[] readBuffer = new Char[256];
try
{
httpWebResponse = (HttpWebResponse)requisicaoWeb.GetResponse();
resposta = httpWebResponse.GetResponseStream();
Encoding enc = Encoding.GetEncoding(1252);
streamRead = new StreamReader(resposta, enc);
}
catch (Exception falha)
{
throw falha;
}
int contador = streamRead.Read(readBuffer, 0, 256);
StringBuilder paginaRequisitada = new StringBuilder();
while (contador > 0)
{
String resultData = new String(readBuffer, 0, contador);
paginaRequisitada.Append(resultData);
contador = streamRead.Read(readBuffer, 0, 256);
}
streamRead.Close();
resposta.Close();
httpWebResponse.Close();
return paginaRequisitada.ToString();
}
Nenhum comentário:
Postar um comentário