Need help with httpwebrequest

J

Jonathan Smith

I am working for a client who needs to be able to log into a website and
download some files. They will not disclose the URL to me, but have given me
the form part of the html code, seen below:

<body>

<form name="frmMain" method="post" action="CsvFiles.aspx" id="frmMain">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwULLTEzNTk2MjQyMzRkZPqs0JpoMJawnQTls76tB64hGmcW" />

<h1>Login</h1>

<div id="LoginPanel">

<table>
<tr>
<td>Username: </td>

<td><input name="Username" type="text" id="Username" /></td>
</tr>
<tr>
<td>Password: </td>
<td><input name="Password" type="password" id="Password" /></td>
</tr>
<tr>
<td> </td>

<td><input type="submit" name="SubmitButton" value="Login"
id="SubmitButton" /></td>
</tr>
</table>


</div>


</form>
</body>

How do i go about constructing a httpwebrequest to login and download the
csv files

Regards

JOnathan
 
J

Johnny Jörgensen

Try this:

Private Sub DoLogin()
Dim response As String =
GetResponse(baseURLOfTheWebsiteHostingTheService & "/CsvFiles.aspx",
"UserName=" & myUserName & "&Password=" & myPassword &
"&SubmitButton=Login")
End Sub

Friend Function GetResponse(ByVal url As String, ByVal data As String) As
String
Dim responseFromServer As String = String.Empty
Dim request As WebRequest = Nothing
Dim response As WebResponse = Nothing
Dim postData As String = data
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
Dim dataStream As Stream = Nothing
Dim reader As StreamReader = Nothing

Try
request = WebRequest.Create(url)
request.Method = "POST"

request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length

dataStream = request.GetRequestStream()

dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()

response = request.GetResponse()

'Console.WriteLine(CType(response,
HttpWebResponse).StatusDescription)
dataStream = response.GetResponseStream()

reader = New StreamReader(dataStream)
responseFromServer = reader.ReadToEnd()
Catch ex As Exception
responseFromServer = String.Empty
Finally
If reader IsNot Nothing Then
reader.Close()
End If
If dataStream IsNot Nothing Then
dataStream.Close()
End If
If response IsNot Nothing Then
response.Close()
End If
If request IsNot Nothing Then
request = Nothing
End If
End Try

Return responseFromServer
End Function

Good Luck,
Johnny J.
 

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