Using WebClient/WebRequest/WebResponse to post postdata and get the result

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?
 
N

naveen

If you are using .net framework 1.1 t might be possible
that in machine.config the following protocols are
disabled(they are disabled by default). Uncomment the
HttpPost and HttpGet and try again.

<protocols>
<add name="HttpSoap1.2"/>
<add name="HttpSoap"/>
<!-- <add name="HttpPost"/> -->
<!-- <add name="HttpGet"/> -->
<add name="HttpPostLocalhost" />
<add name="Documentation"/>
</protocols>

-Naveen
 
Joined
Sep 5, 2005
Messages
1
Reaction score
0
You need to use the "|" character to delimiter the fields for POST...

Dim PostData As Byte() = ASCIIEncoding.GetBytes("Account=" & Account &
"Password=" & Password)

should be:

Dim PostData As Byte() = ASCIIEncoding.GetBytes("Account=" & Account &
"|Password=" & Password & "|")
 
Joined
Nov 1, 2007
Messages
1
Reaction score
0
I got the code working, was wondering if anyone could help me actually post the form? and the password field name wont fill? any advice would be helpfull

Dim WebServiceAccount As String = "accountname"
Dim WebServiceUsername As String = "username"

Dim WebServicePassword As String = "password"

Dim fromURL As String = "http://www.distributesms.com.au/cgi-bin/li.pl"





Dim Request As Net.WebRequest = Net.WebRequest.Create(fromURL)

Request.Method =
"Post"

Request.ContentType = "application/x-www-form-urlencoded"

Dim RequestStream As IO.Stream = Request.GetRequestStream()

Dim ASCIIEncoding As New System.Text.ASCIIEncoding

Dim PostData As Byte() = ASCIIEncoding.GetBytes("&AccountNo=" & WebServiceAccount)

RequestStream.Write(PostData, 0, PostData.Length)

PostData = ASCIIEncoding.GetBytes(
"&Password1=" & WebServicePassword)

RequestStream.Write(PostData, 0, PostData.Length)

PostData = ASCIIEncoding.GetBytes(
"&UserId1=" & WebServiceUsername)

RequestStream.Write(PostData, 0, PostData.Length)

RequestStream.Close()

Dim Reader As New IO.StreamReader(Request.GetResponse().GetResponseStream())

Dim ResultHTML As String = Reader.ReadToEnd

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top