HTTPPOSTsandHTTPGETswithWebClientandC#and
FakingaPostBack
dezembro3,'04PostedinASP.NET|ViewState|Tools
SponsoredBy
Afellowemailedmewantingtoscreenscrape,er,ah,harvestapagethatonlydisplaysthedatahe
wantswithapostback.
RememberwhatanHTTPGETlookslikeunderthecovers:
GET/whatever/page.aspx?param1=value¶m2=value
NotethattheGETincludesnoHTTPBody.That'simportant.WithaPOSTthe'DATA'movesfrom
theQueryStringintotheHTTPBody,butyoucanstillhavestuffintheQueryString.
POST/whatever/page.aspx?optionalotherparam1=value
ContentType:application/xwwwformurlencoded
ContentLength:25
param1=value¶m2=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.