Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Posting to ASP website

Status
Not open for further replies.

jrl237

IS-IT--Management
Jan 29, 2002
61
US
Hi.

I thought this would be easy, but I'm stuck.

I'm trying to create a VB.Net app that will log in to a remote website and upload a file. The web page is an ASP page which I have no control over. It uses a simple Username/Password form which appears to post back to itself.
If I enter the URL for the form upload page, I'm redirected to login.asp, but it remembers the page I requested and redirects to it once I'm authenticated.

From my app I can't get passed the login screen. I'm trying to use the POST method of webrequest, but can't get it to work. Could someone please point me in the right direction?

Any help would be greatly appreciated.

jrl
 
I wish FTP was an option. I'd be done by now. But the only interface is an asp page.
 
Try something like this:

Call WebRequest.Create to get an instance of a HttpWebRequest. Set your contenttype to "application/x-
You then call GetRequestStream to get a Stream object to write your request. Write your encoded request to the stream (using a TextStream or similar).

You then call GetResponse which returns you a WebResponse object.

Read more at:
[link ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemNetHttpWebRequestClassGetRequestStreamTopic.htm]ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemNetHttpWebRequestClassGetRequestStreamTopic.htm[/url]

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks for the info, Chip.

I've used the httpWebRequest object successfully in the past. That's why I thought this would be easy. Were I'm having problems is getting it to accept the login. Seems like I should be able to create the request such as "Username=USER&Password=PASSWORD" where Username and Password are the names of the input fields on the ASP page, and USER and PASSWORD are what I use to login.

I submit the above, but all I ever get from GetResponse is the login page back.

Here's some code I'm trying to work with:

Private Sub getPage(ByVal url As String, ByVal payload As String)
Dim req As WebRequest
Dim RequestStream As Stream
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
Dim sHTML As String

req = WebRequest.Create(url)
req.Method = "POST"
req.ContentType = "application/x- Dim SomeBytes() As Byte
Dim UrlEncoded As New StringBuilder()
Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}

Dim i As Integer = 0
Dim j As Integer
While i < payload.Length
j = payload.IndexOfAny(reserved, i)
If j = -1 Then
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length - i)))
Exit While
End If
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j - i)))
UrlEncoded.Append(payload.Substring(j, 1))
i = j + 1
End While
SomeBytes = System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToString())
req.ContentLength = SomeBytes.Length
RequestStream = req.GetRequestStream()
RequestStream.Write(SomeBytes, 0, SomeBytes.Length)
RequestStream.Close()
result = req.GetResponse()
ReceiveStream = result.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(ReceiveStream, encode)

sHTML = sr
' .
' Process HTML HERE
' .
End Sub

I pass it the URL and the payload. payload="Username=USER&Password=PASS"

This is someone else's code, but I think I understand it fairly well. I just can't make it work.
 
I accidentally cut a line out of the above code when posting. It should have:

Dim response as Webresponse

at the top.
 
Try that one more time. It should have:

Dim result as Webresponse

at the top.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top