2014-07-17 2 views
1

Basecamp API를 사용하여 FTP 사이트에서 Basecamp로 파일을 업로드하려고합니다. 간단한 콘솔 응용 프로그램을 사용하고 있습니다. 여기에 내 코드입니다 :WebClient.UploadData "기본 연결이 닫혔습니다."

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 protected])") 
    '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에서 바이트 배열로 파일을 업로드하고 싶습니다.

모든 의견을 크게 기뻐할 것입니다. 감사!

+0

위의 코드는 컴퓨터에서 실행할 때 작동하지만 FTP 서버에서 실행할 때 작동하지 않는다고 말하고 있습니까? 특정 HTTP 응답 코드 또는 연결이 닫힌 것 외의 메시지가 닫히고 있습니까? – mclark1129

+0

오늘은 내 컴퓨터에서 실행 중이었지만 어느 시점에 내 컴퓨터에서 연결 종료 메시지를 받기 시작했습니다. 연결이 닫힌 것 외의 다른 HTTP 응답 코드는 보지 못했습니다. – Jarod

답변

1

WebClient 호출에서 무엇이 잘못되었는지를 결코 알지 못했지만 https://basecampwrapper.codeplex.com에서 Basecamp API 래퍼를 사용했습니다. 해당 래퍼는 WebClient.UploadData 대신 HTTPRequest 및 HTTPResponse를 사용합니다. 또한 처음부터 내 코드를 작성하는 것보다 랩퍼를 사용하는 것이 훨씬 쉽습니다.