Vb.net WebClient.UploadData“;“基础连接已关闭”;
我正在尝试使用Basecamp API将一个文件从FTP站点上传到Basecamp。我正在使用一个简单的控制台应用程序。这是我的密码:Vb.net WebClient.UploadData“;“基础连接已关闭”;,vb.net,webclient,basecamp,Vb.net,Webclient,Basecamp,我正在尝试使用Basecamp API将一个文件从FTP站点上传到Basecamp。我正在使用一个简单的控制台应用程序。这是我的密码: Try Dim accountID As String = ConfigurationManager.AppSettings("BaseCampID") Dim projectID As Integer = 9999999 Dim folderName As String = "XXXXX/XXXXX" Dim fileName
Try
Dim accountID As String = ConfigurationManager.AppSettings("BaseCampID")
Dim projectID As Integer = 9999999
Dim folderName As String = "XXXXX/XXXXX"
Dim fileName As String = "XXX.zip"
'The URL to access the attachment method of the API
Dim apiURL = String.Format("https://basecamp.com/{0}/api/v1/projects/{1}/attachments.json", accountID, projectID)
'Get the file from the FTP server as a byte array
Dim fileBytes As Byte() = GetFileBytes(String.Format("{0}\\{1}", folderName, fileName))
'Initialize the WebClient object
Dim client As New WebClient()
client.Headers.Add("Content-Type", "application/zip")
'Need to provide a user-agent with a URL or email address
client.Headers.Add("User-Agent", "Basecamp Upload (email@email.com)")
'Keep the connection alive so it doesn't close
client.Headers.Add("Keep-Alive", "true")
'Provide the Basecamp credentials
client.Credentials = New NetworkCredential("username", "password")
'Upload the file as a byte array to the API, and get the response
Dim responseStr As Byte() = client.UploadData(apiURL, "POST", fileBytes)
'Convert the JSON response to a BaseCampAttachment object
Dim attachment As BaseCampAttachment
attachment = JSonHelper.FromJSon(Of BaseCampAttachment)(Encoding.Default.GetString(responseStr))
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
Console.ReadLine()
End Try
但是,每当它调用client.UploadData时,我都会收到错误消息“底层连接已关闭:连接意外关闭”。我之前遇到过这个问题,并认为通过添加“Keep Alive”头解决了这个问题,但它不再工作了。如果我使用client.UploadFile上传本地文件,API就可以工作,但我只想从FTP的字节数组上传文件,而不是本地下载文件然后上传到Basecamp
任何想法都将不胜感激。谢谢 我从未发现WebClient调用有什么问题,但最终使用了来自的Basecamp API包装器。该包装器使用HTTPRequest和HTTPResponse而不是WebClient.UploadData。仅仅使用包装器也比从头开始编写自己的代码容易得多。那么您是说,当您在计算机上运行上述代码时,这些代码可以工作,但当您在FTP服务器上运行时,这些代码就不能工作了?除了连接关闭之外,您是否收到任何特定的HTTP响应代码或消息?它在当天早些时候在我的机器上运行,但在某个时候它开始在我的机器上接收连接关闭消息。除了连接关闭之外,我没有看到任何其他HTTP响应代码。