|
-
May 23rd, 2013, 02:17 AM
#1
Thread Starter
Fanatic Member
httpwebrequest - Clarification
Hi
ive just started going over this class and gone through a couple of walkthroughs at the top of google, and i understand most of it but i think there is alot of
miss-information im being fed.
heres the best sample i found (with youtube tutorial (@ howtostartprogramming.com)thx) , im just not sure about a couple of things
ill post some code and mark in red what i have questions about.(i replaced the websites)
Code:
Dim postData As String = "http://www.abc.com"
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://www.abc.com/search?name=bob"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "http://www.abc.com/search?name=bob"
postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
page = postreqreader.ReadToEnd
ok now the website urls where different i just changed them.
the first red line, im thinking its basically the page your are going to get the data for. is that right.
the second red line here could be anything, but in this instance lets say its passing a request as if a search button was pressed with bob in a textbox, is that right.
..........POINT 1 - why would i even need the first red line, is it necessary? i could just send the request right away??!!
third red line, do i really need to set keepalive = true, what if i try and access a bad address, or a server with a serious system hang, it wont time out?????
fourth red line, will the content type change alot or is this usually ok
fifth line, i heard that some sites may need this setting for example with redirects, but is it neccessary normally with a basic site.
and finally what i really dont get is
the sixth line, why is the postrequest.contentlength being set by the length of postdata(ie the site address), should it not be postreq.length????
sorry for the mess
thanks
Yes!!!
Working from home is so much better than working in an office...
Nothing can beat the combined stress of getting your work done on time whilst
1. one toddler keeps pressing your AVR's power button
2. one baby keeps crying for milk
3. one child keeps running in and out of the house screaming and shouting
4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
5. working at 1 O'clock in the morning because nobody is awake at that time
6. being grossly underpaid for all your hard work

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|