简体   繁体   中英

Programmatically create an http file in new http folder in vb.net

I need to dynamically create a http file specific to each desktop app user so that I can redirect him/her to the created url when on a button is clicked in the app. So it has to be http, not ftp.

The file's folder also needs to be created on the fly.

I started by trying to create/upload a file into an existing http folder.

Attempt [1] System.Net.WebException: 'The remote server returned an error: (404) Not Found.'

  Dim client As System.Net.WebClient = New System.Net.WebClient()
  client.Credentials = New System.Net.NetworkCredential("user", "pass")
  client.UploadFile("httpWwwDomainDotCom/someFolder/destinationFile.txt", 
    "c:\users\someUser\desktop\sourceFile.txt")      
  client.Dispose()

Attempt [2] The following code gives me a success message but no file is being created!

 Try
     Dim request As System.Net.HttpWebRequest = 
DirectCast(System.Net.HttpWebRequest.Create("httpWwwDomainDotCom/someFolder/destinationFile.txt"), System.Net.HttpWebRequest)

     request.Credentials = New System.Net.NetworkCredential("user", "pass")
     request.Method = System.Net.WebRequestMethods.Http.Post

     Dim file() As Byte = System.IO.File.ReadAllBytes("c:\users\someUser\desktop\sourceFile.txt")

     Dim stream As System.IO.Stream = request.GetRequestStream()
     stream.Write(file, 0, file.Length)

     stream.Close()
     stream.Dispose()

     MsgBox("Http uploading succeeded!")

  Catch ex As Exception
     MsgBox("Http uploading failed!")
  End Try 

Any help much appreciated.

I ended up doing the following:

  • upload the file to ftp folder
  • trigger a web page with php code in it to copy the uploaded file to http folder

That php page, I could call from my desktop application in the background with this code:

Dim phpFile As String = "http://someDomain/copy.php" 
Dim response As WebResponse
Dim request As WebRequest = HttpWebRequest.Create(phpFile)
response = request.GetResponse()

Can't believe one cannot just upload files to an http address using credentials!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM