J
Jonathan Amend
I'm having a lot of trouble using the objects provided in System.Net to
download a page using the POST method. WebClient can send a querystring to a
page but as far as I know there's no way of getting it to use the POST
method. I tried using WebClient.UploadData() and it can download a page fine
but for some reason it doesn't send the postdata. I then tried the more
complicated way using WebRequest, WebResponse, and IO Streams/Readers but it
doesn't post the postdata either. I tried looking for examples in the MSDN
library and on PSC but the only example on PSC has the same results and the
MSDN library seems to be a bit incomplete/erroneous
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemnetwebrequestclassgetrequeststreamtopic.asp ... the example is
missing). Here are the 3 methods that I tried:
Method 1: QueryString (GET only)
Dim WebClient As New System.Net.WebClient
WebClient.QueryString.Add("Account", Account)
WebClient.QueryString.Add("Password", Password)
Dim Reader As New IO.StreamReader(WebClient.OpenRead(LogInURL))
Dim ResultHTML = Reader.ReadToEnd
Method 2: UploadData
Dim WebClient As New System.Net.WebClient
WebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Dim ASCIIEncoding As New System.Text.ASCIIEncoding
Dim PostData As Byte() = ASCIIEncoding.GetBytes("Account=" & Account &
"Password=" & Password)
Dim ResultHTML As String =
ASCIIEncoding.GetChars(WebClient.UploadData(LogInURL, "POST", PostData))
Method 3: WebRequest/WebResponse
Dim Request As Net.WebRequest = Net.WebRequest.Create(LogInURL)
Request.Method = "POST"
Request.ContentType = "application/x-www-form-urlencoded"
Dim RequsetStream As IO.Stream = Request.GetRequestStream()
Dim ASCIIEncoding As New System.Text.ASCIIEncoding
Dim PostData As Byte() = ASCIIEncoding.GetBytes("Account=" & Account &
"Password=" & Password)
RequsetStream.Write(PostData, 0, PostData.Length)
RequsetStream.Close()
Dim Reader As New IO.StreamReader(Request.GetResponse().GetResponseStream())
Dim ResultHTML As String = Reader.ReadToEnd()
The first method works on a page that processes GET querystrings but not on
ones that process postdata from POST methods and the other 2 don't work at
all. Am I using the wrong methods or am I just using them in the wrong way?
download a page using the POST method. WebClient can send a querystring to a
page but as far as I know there's no way of getting it to use the POST
method. I tried using WebClient.UploadData() and it can download a page fine
but for some reason it doesn't send the postdata. I then tried the more
complicated way using WebRequest, WebResponse, and IO Streams/Readers but it
doesn't post the postdata either. I tried looking for examples in the MSDN
library and on PSC but the only example on PSC has the same results and the
MSDN library seems to be a bit incomplete/erroneous
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemnetwebrequestclassgetrequeststreamtopic.asp ... the example is
missing). Here are the 3 methods that I tried:
Method 1: QueryString (GET only)
Dim WebClient As New System.Net.WebClient
WebClient.QueryString.Add("Account", Account)
WebClient.QueryString.Add("Password", Password)
Dim Reader As New IO.StreamReader(WebClient.OpenRead(LogInURL))
Dim ResultHTML = Reader.ReadToEnd
Method 2: UploadData
Dim WebClient As New System.Net.WebClient
WebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Dim ASCIIEncoding As New System.Text.ASCIIEncoding
Dim PostData As Byte() = ASCIIEncoding.GetBytes("Account=" & Account &
"Password=" & Password)
Dim ResultHTML As String =
ASCIIEncoding.GetChars(WebClient.UploadData(LogInURL, "POST", PostData))
Method 3: WebRequest/WebResponse
Dim Request As Net.WebRequest = Net.WebRequest.Create(LogInURL)
Request.Method = "POST"
Request.ContentType = "application/x-www-form-urlencoded"
Dim RequsetStream As IO.Stream = Request.GetRequestStream()
Dim ASCIIEncoding As New System.Text.ASCIIEncoding
Dim PostData As Byte() = ASCIIEncoding.GetBytes("Account=" & Account &
"Password=" & Password)
RequsetStream.Write(PostData, 0, PostData.Length)
RequsetStream.Close()
Dim Reader As New IO.StreamReader(Request.GetResponse().GetResponseStream())
Dim ResultHTML As String = Reader.ReadToEnd()
The first method works on a page that processes GET querystrings but not on
ones that process postdata from POST methods and the other 2 don't work at
all. Am I using the wrong methods or am I just using them in the wrong way?