FtpWebRequest.GetRequestStream 挂起并失败。

发布于 2024-11-27 10:25:23 字数 1793 浏览 1 评论 0原文

我写了一个网络服务,简而言之,它使用 openpop 来获取电子邮件,将内容插入数据库并保存图像附件。当我在本地保存图像时,效果很好,它完全符合预期。现在,一个附加要求是将图像保存到 FTP 目录,这样我就可以动态创建文件夹(它们是根据时间戳创建的),而且效果很好。我的问题来自于我尝试将它们保存到 ftp 时。是的,我的用户名和密码是正确的,否则我不会创建该目录。

Private Sub UploadFile(ByVal fileToSave As FileInfo, ByVal path As String)
    Dim UploadRequest As FtpWebRequest = DirectCast(WebRequest.Create("ftp://UserName:[email protected]" & path), FtpWebRequest)
    UploadRequest.Credentials = New NetworkCredential("PicService", "grean.matching18")
    UploadRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    UploadRequest.UseBinary = True
    UploadRequest.UsePassive = True

    ' Const BufferSize As Integer = 2048
    ' Dim content(BufferSize - 1) As Byte, dataRead As Integer
    Dim bFile() As Byte = System.IO.File.ReadAllBytes(fileToSave.ToString)
    'UploadRequest.ContentLength = content.Length

    Using FileStream1 As FileStream = fileToSave.OpenRead()
        Try
            'open request to send
            Using RequestStream As Stream = UploadRequest.GetRequestStream

            End Using
        Catch ex As Exception
        Finally
            'ensure file closed
            FileStream1.Close()
        End Try
    End Using
End Sub

我也尝试过使用 Passive False 和 Binary False,我对堆栈跟踪做了更多研究。 并发现这篇文章< /a> 但目前还没有解决方案。如有任何意见,我将不胜感激,我还针对不同问题发布了有关 Windows 服务的另一个问题。如果您想尝试一下,另一个问题不是关于 ftp 而是关于 Windows Server 2003 上的服务的权限

I have wrote a web service, in a nutshell it uses openpop to get email messages does stuff with the content to insert into databases and saves attachments which are images. That works fine when i save images locally, it does exactley what it is suppose to. Now an added requirment was to save images to an FTP directory, so i can create my folders dynamically (they are created based upon timestamp) and that works well. My problem comes from when i try to save them to the ftp. Yes my user name and password are correct, otherwise i wouldn't be creating the directory.

Private Sub UploadFile(ByVal fileToSave As FileInfo, ByVal path As String)
    Dim UploadRequest As FtpWebRequest = DirectCast(WebRequest.Create("ftp://UserName:[email protected]" & path), FtpWebRequest)
    UploadRequest.Credentials = New NetworkCredential("PicService", "grean.matching18")
    UploadRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    UploadRequest.UseBinary = True
    UploadRequest.UsePassive = True

    ' Const BufferSize As Integer = 2048
    ' Dim content(BufferSize - 1) As Byte, dataRead As Integer
    Dim bFile() As Byte = System.IO.File.ReadAllBytes(fileToSave.ToString)
    'UploadRequest.ContentLength = content.Length

    Using FileStream1 As FileStream = fileToSave.OpenRead()
        Try
            'open request to send
            Using RequestStream As Stream = UploadRequest.GetRequestStream

            End Using
        Catch ex As Exception
        Finally
            'ensure file closed
            FileStream1.Close()
        End Try
    End Using
End Sub

I have tried using Passive False and Binary False as well, i did more research on my stack trace.
And found this article but no solution as of yet. Any input would be appreciated, i am also posting another question on windows services for different issue. If you would like to take a shot at it, the other question isnt about ftp but permissions for a service on windows server 2003

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2024-12-04 10:25:23

这可能不是解决方案,但我发现 URI 字符串必须“恰到好处”,并且“恰到好处”因 ftp 服务器而异。

因此,ftp://server/directory/file 在某些服务器上工作,但需要 ftp://server//directory/file 才能在其他服务器上工作(注意双服务器名称后的斜杠)

麻生太郎,您的 URI 的“密码”拼写错误: ftp://UserName:[电子邮件受保护] 并且您也在单独的代码行中提供凭据。

This may not be the solution but I've found that the URI string has to be 'just right' and that what is 'just right' varies by the ftp server.

So ftp://server/directory/file works on some servers but needs to be ftp://server//directory/file to work on others (note the double slash after the server name)

Aso, your URI has 'password' spelled incorrectly: ftp://UserName:[email protected] and you are supplying the credentials in a separate code line as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文