繁体   English   中英

在vb.net的新http文件夹中以编程方式创建一个http文件

[英]Programmatically create an http file in new http folder in vb.net

我需要动态创建一个针对每个桌面应用程序用户的http文件,以便在应用程序中单击按钮时可以将他/她重定向到创建的url。 因此它必须是http,而不是ftp。

还需要即时创建文件的文件夹。

我首先尝试将文件创建/上传到现有的http文件夹中。

尝试[1] System.Net.WebException:'远程服务器返回错误:(404)未找到。'

  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()

尝试[2]以下代码给我成功消息,但未创建文件!

 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 

任何帮助,不胜感激。

我最终做了以下工作:

  • 将文件上传到ftp文件夹
  • 触发其中包含php代码的网页,将上传的文件复制到http文件夹

那个php页面,我可以使用以下代码从后台的桌面应用程序调用:

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

简直无法相信只能使用凭据将文件上传到http地址!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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