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 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」ヘッダーを追加して解決したと思っていましたが、機能しなくなりました。API は、client.UploadFile を使用してローカル ファイルをアップロードすると機能しますが、ファイルをローカルにダウンロードしてから Basecamp にアップロードするのではなく、FTP からバイト配列からファイルをアップロードしたいだけです。
どんな考えでも大歓迎です。ありがとう!