Page 1 of 1

Problem Reading Webpage

Posted: Sun Oct 31, 2010 3:15 am
by 2cool4cereal2
To start off, I really don't know if I am posting this in the correct spot, so please feel free to move it accordingly if not.

Ok, I am trying to allow users to sign into my program using a sign in form I have created, but I am having a problem detecting whether or not a user already exists. I'm basically using the coding below, but am getting errors.

Code: Select all
 
  Try
            If MessageBox.Show("You may only create one account. Are you sure you want to continue?", _
                           "New Account", _
                           MessageBoxButtons.OKCancel, _
                                   MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK Then


                If Not TextBox2.Text = TextBox3.Text Then
                    MsgBox("Your passwords did not match.")
                Else
                                            V------PROBLEM STARTS HERE----V
                    Dim url As String = "http://mywebsite.com/" + TextBox1.Text + ".accnt"
                    Dim pageRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
                    Dim pageResponse As WebResponse = pageRequest.GetResponse()
                    Dim page As String = ""
                    Using r As New StreamReader(pageResponse.GetResponseStream())
                        page = r.ReadToEnd()
                    End Using
                                           ^------PROBLEM ENDS HERE (Kinda)----^
            If Not page = TextBox1.Text & vbNewLine & "Account Exists" Then
                If MessageBox.Show("Do you wish to create this new account?", _
                   "New Account", _
                   MessageBoxButtons.OKCancel, _
                           MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK Then
'Create account
The problem is that an error occurs because the reader cannot find the page... But the point is for it NOT to find the page. If it does find the page, then that means the user name already exists, but if it does not, the new user can be created. Anyone have any suggestions, because I'm completely out of ideas.... dunnno;

Re: Problem Reading Webpage

Posted: Sun Oct 31, 2010 12:48 pm
by mandai
You should use a try/catch block when creating the HttpWebRequest if you want to check for missing files.

Re: Problem Reading Webpage

Posted: Sun Oct 31, 2010 12:53 pm
by bisnes_niko
Ugh...
Code: Select all
TRY
Dim WCL as new net.webclient
WCL.Credentials = New Net.NetworkCredential("username", "password") ' Not neccesary if they are in public folder
DIM AccountInformation() as String = WCL.DownloadString("www.mysite.com/" & textbox1.text & ".account").Split(";")
msgbox("Username: " & AccountInformation(0) & ", " & "Password: " & AccountINformation(1))
Catch ex as Exception
msgbox("Wrong username or password.") 
END TRY
The file on the server should be like "username;password", otherwise it doesnt work.

Are you having somekind of login system? :p

Re: Problem Reading Webpage

Posted: Sun Oct 31, 2010 5:11 pm
by 2cool4cereal2
macHard wrote:
Ugh...
Code: Select all
TRY
Dim WCL as new net.webclient
WCL.Credentials = New Net.NetworkCredential("username", "password") ' Not neccesary if they are in public folder
DIM AccountInformation() as String = WCL.DownloadString("www.mysite.com/" & textbox1.text & ".account").Split(";")
msgbox("Username: " & AccountInformation(0) & ", " & "Password: " & AccountINformation(1))
Catch ex as Exception
msgbox("Wrong username or password.") 
END TRY
The file on the server should be like "username;password", otherwise it doesnt work.

Are you having somekind of login system? :p
Thanks for the response, but I'll try this out, but I'm not quite understanding how it works... Could you post an explanation also? Or, just tell me if I have it right...
So the 2nd line takes care of logging into my FTP server. (I understand that.)
The 3rd line.... Ummm.... TRIES downloading the information in textbox1?
Then the 4th line.... Shows the username and password?
This doesn't look too bad overall, but I am trying to use this code when creating a new user, not necessarily to create a new user. I think I'm just misunderstand the code. If you'd respond to this post I'd be very thankful. :)

Thanks for your help. :)

Re: Problem Reading Webpage

Posted: Wed Nov 03, 2010 6:35 pm
by bisnes_niko
Hey, I think you understood it the right... the msgbox username & password was just an example... this way goes that

A username inputs his username + password. The login will try to download the account authentication file from the server (site/accounts/username.account)

If it doesn't appear in the server, it will cause an error.. (reason I used TRY, Catch Ex, END try

IF the account exists, it will read the file (and may download into temporary folder?) If the AccountInformation(1) (Password) IS the textbox2.text then the login is succesful, otherwise give an message "wrong username or password"

Hope you understood a bit...

Now for the registering new account:

You need an WebClient and FTP Server authentication (for uploading the file)
Heres what it should look like
Code: Select all
dim FTPServer as String = "ftp://yoursite.com/" ' Just to make things smaller
Dim WCL as new net.Webclient ' Our Webclient
WCL.Credentials = New Net.NetworkCredential("username", "password" ' Required for uploading file

' Now we need to upload the account file on the web server
 wcl.UploadString(FTPServer & "username.account", textbox1.text & ";" & textbox2.text)

' Now it has created the file on ftp server and lets inform the user
msgbox("account registered!")
form2.show ' or not?
I think this is what you are after... unless you didn't understand or something, I have my skype bisnes_niko or if u just wanna chat with me ^^