download file from https-site with username and password

Hi,

i want to download a file (it’s a sqlite-db) from a htaccess-protected folder of my webserver to a folder.

It’s a https-site, so the HTTPSecureSocket seems to be the right choice. But how do i implement the username and password? how do i start the download? The xojo.doc did not show me the right direction.

Can anyone light up my way?

Thanks

Michael

Michael,

You have to use the AuthenticationRequired event in the http://documentation.xojo.com/index.php/HTTPSecureSocket and then return true.

This is where you pass your name/password. Also be sure to know what version of SSL/TLS you need to use also.

Good luck!

for example:
AuthenticationRequired Event:

  Name = myUsername
  Password = myPassword
  Return True

In addition within the constructor of the HTTPSecureSocket I will generally set some other User Agent specific parameters. This can vary per platform/browser that you are wishing to emulate.

    Self.SetRequestHeader("Accept", "*/*")
    Self.SetRequestHeader("Accept-Encoding:","gzip,deflate,sdch")
    Self.SetRequestHeader("User-Agent:","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36")
    Self.SetRequestHeader("Connection", "keep-alive")
    Self.SetRequestHeader("Accept-Language", "en-US,en;q=0.8")

The following I also include in the same constructor to set my port information. Notice ConnectionType=3 (TLSv1.0).

  Self.Secure = True
  Self.port = 443
  Self.ConnectionType = 3 // TLSv1

You can also set the authorization header before sending the request.

MyHTTPSecureSocket.SetRequestHeader("Authorization", "Basic " + EncodeBase64("MYUSERNAME" + ":" + "MYPASSWORD"))

One way to address this is to subclass the HTTPSecureSocket class and give it Username and Password properties. You can then set those before you connect and implement the event to take the username and password from them. If they aren’t filled in, you can return false.

Thank you all.

I tried Andrews example and i can download the file. Just have to add some Errorhandling and it does what i need.

Perfect.

Michael

Andrews solutions works fine, i get the file, but with different size every time. how can i check that the file is downloaded completely?

HTTPSocket.DownloadComplete?

Now i use this code to download a file via https from a protected site:

[code] Dim f As FolderItem

f = SpecialFolder.ApplicationData.Child(“UniFormis”)
f = f.Child(“uniformis.sqlite”)

dim downloadSocket As New HTTPSecureSocket

downloadSocket.SetRequestHeader(“Authorization”, "Basic " + EncodeBase64(“USERNAME” + “:” + “PASSWORD”))
if downloadSocket.Get(“https://PATH-TO-DOWNLOAD-FILE/uniformis.sqlite”, f, 30) = True then
MsgBox(“Download complete”)
end if[/code]

I do get a file every time i run this code, but the file is to small, so my sqlite-db is not working. i only get 47 bytes, the original-filesize is 124 Kbytes. I also reach the download-complete status.

I have tried 2 ways:

  1. Drop a tcp-socket (with superclass HTTPSecureSocket) in my window and use the code above without the dim downloadSocket as new HTTPSecureSocket statement

  2. Remove the tcp-socket from my window and create it with the dim downloadSocket as new HTTPSecureSocket Statement

Both methods download a file (ok, maybe they only create a file) and both do not drop an error-message.

Do i have to place a HTTPSecureSocket-Control on my windows to get this working?

Michael

i now made a separate download window, like in the xojo-examples. i placed a HTTPSecureSocket-Control (downloadSocket), a Progressbar (downloadProgress) and a DownloadButton (btnDownload) on this new window.

When the DownloadButton is pressed the method DownloadSqliteDB is called:

[code] Dim downloadFile As FolderItem = SpecialFolder.ApplicationData.Child(“UniFormis”).Child(“uniformis.sqlite”)

downloadSocket.Secure = True
downloadSocket.Port = 443
downloadSocket.ConnectionType = 3

downloadSocket.Get(“https://PATH-TO-FILE/uniformis.sqlite”, downloadFile)[/code]

Within the downloadSocket there are the following functions:

[code]AuthenticationRequired

Name=USERNAME
Password=PASSWORD[/code]

[code]receiveProgress

Dim prozent As Integer = (bytesReceived / totalBytes) * 100
lblDownload.Text = “Download in progress…”
downloadProgress.Value = prozent[/code]

[code]DownloadComplete

lblDownload.Text = “Download complete.”
downloadProgress.Value = downloadProgress.Maximum
MsgBox(“Download complete.”)
WndDownload.Close[/code]

When i press the button, there is a uniformis.sqlite-file in my folder, but only 47 bytes large. the “download complete” Message comes immediately and the ProgressBar is at 100 %.

Wheres my fault? This looks to me exactly like the Xojo-Example.

Any further hints?

Michael

Did you examine the contents of the file in a text editor?

Sorry, found my fault: i missed a Return True in the AuthenticationRequired-Function. Now it works.

Thanks again.

How did you create HTTPSecureSocket on the IDE?. I can only create it by code, but at this mode I don’t know how to use events AuthenticationRequired. Thanks

I think i found the answer to my question: https://forum.xojo.com/5075-httpsecuresocket-ide/0

To use the events of a control created in code you can use AddHandler :slight_smile:
http://documentation.xojo.com/index.php/AddHandler

[quote=231662:@Albin Kiland]To use the events of a control created in code you can use AddHandler :slight_smile:
http://documentation.xojo.com/index.php/AddHandler[/quote]
Thanks men!

Did you have an example how to use an AddHandler for HTTPSecureSocket, I’m trying to deal with DownloadComplete event of the Handler.

I have several sockets created in code in a array of sockets.

I think that this is the way do deal with AddHandler:

AddHandler NombreSockets(0).DownloadComplete, AddressOf  ???

But I dunno whats next. :(. :frowning:

[quote=343871:@Gerardo García]AddHandler NombreSockets(0).DownloadComplete, AddressOf ???

But I dunno whats next. :(. :([/quote]

What’s next is the name of the method that you want to have handle the event. This method has to match the signature of the event it’s handling, and in addition receives a reference to the object that’s raising the event as the first parameter.

i.e. The DownloadComplete event is defined like this:

Sub DownloadComplete(URL as String, HTTPStatus as Integer, Headers as InternetHeaders, File as FolderItem)

So you need to write a method that looks like this:

Sub MyHandler(Sender As HTTPSecureSocket, URL as String, HTTPStatus as Integer, Headers as InternetHeaders, File as FolderItem)

And then use it with AddressOf:

AddHandler NombreSockets(0).DownloadComplete, AddressOf MyHandler

[quote=343871:@Gerardo García]Did you have an example how to use an AddHandler for HTTPSecureSocket, I’m trying to deal with DownloadComplete event of the Handler.

I have several sockets created in code in a array of sockets.

I think that this is the way do deal with AddHandler:

AddHandler NombreSockets(0).DownloadComplete, AddressOf  ???

But I dunno whats next. :(. :([/quote]
Sorry, been away for a while, but it seems Andrew gave a good explanation :slight_smile: