0% found this document useful (0 votes)
54 views3 pages

Simular Webrequest Com C#

This document discusses using HTTP GET and POST requests in C# to scrape or harvest data from web pages. It provides code examples for making GET and POST requests using the WebRequest class. A POST request allows sending form data in the HTTP request body rather than the query string. The document explains how to fake a POST request to a page that requires postback data in order to retrieve the desired data, noting some limitations around viewstate.

Uploaded by

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

Simular Webrequest Com C#

This document discusses using HTTP GET and POST requests in C# to scrape or harvest data from web pages. It provides code examples for making GET and POST requests using the WebRequest class. A POST request allows sending form data in the HTTP request body rather than the query string. The document explains how to fake a POST request to a page that requires postback data in order to retrieve the desired data, noting some limitations around viewstate.

Uploaded by

dauto_nunes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HTTPPOSTsandHTTPGETswithWebClientandC#and

FakingaPostBack
dezembro3,'04PostedinASP.NET|ViewState|Tools
SponsoredBy

Afellowemailedmewantingtoscreenscrape,er,ah,harvestapagethatonlydisplaysthedatahe
wantswithapostback.
RememberwhatanHTTPGETlookslikeunderthecovers:
GET/whatever/page.aspx?param1=value&param2=value
NotethattheGETincludesnoHTTPBody.That'simportant.WithaPOSTthe'DATA'movesfrom
theQueryStringintotheHTTPBody,butyoucanstillhavestuffintheQueryString.
POST/whatever/page.aspx?optionalotherparam1=value
ContentType:application/xwwwformurlencoded
ContentLength:25

param1=value&param2=value
NotetheContentTypeheaderandtheContentLength,thoseareimportant.
APOSTisjusttheverbforwhenyouhaveanHTTPdocument.AGETimpliesyougotnothing.
So,inC#,here'saGET:
publicstaticstringHttpGet(stringURI)
{
System.Net.WebRequestreq=System.Net.WebRequest.Create(URI)
req.Proxy=newSystem.Net.WebProxy(ProxyString,true)//truemeansnoproxy
System.Net.WebResponseresp=req.GetResponse()
System.IO.StreamReadersr=newSystem.IO.StreamReader(resp.GetResponseStream())
returnsr.ReadToEnd().Trim()
}

Here'saPOST:
publicstaticstringHttpPost(stringURI,stringParameters)
{
System.Net.WebRequestreq=System.Net.WebRequest.Create(URI)
req.Proxy=newSystem.Net.WebProxy(ProxyString,true)
//Addthese,aswe'redoingaPOST
req.ContentType="application/xwwwformurlencoded"
req.Method="POST"
//Weneedtocounthowmanybyteswe'resending.Post'edFakedFormsshouldbename=value&
byte[]bytes=System.Text.Encoding.ASCII.GetBytes(Parameters)
req.ContentLength=bytes.Length
System.IO.Streamos=req.GetRequestStream()
os.Write(bytes,0,bytes.Length)//Pushitoutthere
os.Close()
System.Net.WebResponseresp=req.GetResponse()
if(resp==null)returnnull
System.IO.StreamReadersr=newSystem.IO.StreamReader(resp.GetResponseStream())
returnsr.ReadToEnd().Trim()
}

Icouldandshouldhaveputinmore'using'statements,butyougetthegist.And,thereareotherways
tohavedonethiswiththeBCL,butthisisone.
Now,howwouldyoufakeanHTTPPostBack?UseatoollikeieHttpHeaderstowatchwhatareal
postbacklookslike,andwell,fakeit.:)Justhopetheydon'trequireunique/encryptedViewState(via
ViewStateUserKeyorEnableViewStateMac)forthatpage,oryou'reoutofluck.
AboutScott
ScottHanselmanisaformerprofessor,formerChiefArchitectinfinance,nowspeaker,consultant,
father,diabetic,andMicrosoftemployee.Heisafailedstandupcomic,acornrower,andabook
author.

AboutNewsletter
SponsoredBy

O Dlar Subir Mais?


A Tendncia de
Alta do Dlar
Vai Durar At
Quando? Saiba
Agora, Aqui

HostingBy

Disclaimer:Theopinionsexpressedhereinaremyownpersonalopinionsanddonotrepresentmy
employer'sviewinanyway.

You might also like