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
Re: httpwebrequest - Clarification
Answering your specific questions first:
First red line: look at the variable name: "postData" - this is the data that you will be POSTing to the resource you are requesting.
Second red line: this creates the request, and the string is the URL of the resource you are requesting (i.e. "the page you are going to get the data for")
Point 1: You are going to send the data you set up in the first red line in the request. After the first red line, it converts the string to a byte representation that it later uses for the content of the request.
Third red line: this is not related to timing out the request. It's the HTTP keep=alive flag. If this is true, then it is instructing the client and server to keep the TCP connection open for subsequent requests.
Fourth red line: The content type will change depending on what the content is. Normally you wouldn't need content (GETs); when you are sending content, it will be determined by the type and format of the content.
Fifth red line: Certain websites may depend on the referer header, but since it can be easily spoofed and is sometimes not present, it isn't good practice to rely on it being there for proper operation of the site.
Sixth red line: The content of the request is the POST data. The Subsequent lines set up the "Request Stream" to contain the byte array. This is what the content length header is referring to.
I think you need to perhaps read up on the architecture of the web - what is a Resource, what do the different HTTP methods mean, etc. You're showing a bit of confusion on some of the basics.