首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebClient.UploadData“基础连接已关闭”

WebClient.UploadData“基础连接已关闭”
EN

Stack Overflow用户
提问于 2014-07-17 17:28:15
回答 1查看 729关注 0票数 1

我正在尝试使用Basecamp API将一个文件从FTP站点上传到Basecamp。我正在使用一个简单的控制台应用程序。这是我的密码:

代码语言:javascript
复制
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时,我都会收到错误消息“基础连接已关闭:连接意外关闭”。我之前遇到了这个问题,并认为我通过添加“保持活着”的标题来解决这个问题,但是它不再起作用了。如果我用client.UploadFile上传本地文件,API就能工作,但我想从它们中上传文件,而不是在本地下载文件,然后将文件上传到Basecamp。

任何想法都将不胜感激。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-18 13:24:33

我从来不知道WebClient调用有什么问题,但我最终使用了来自https://basecampwrapper.codeplex.com的Basecamp包装器。该包装器使用HTTPRequest和HTTPResponse,而不是WebClient.UploadData。仅仅使用这个包装程序也比从头开始编写我自己的代码容易得多。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24809901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档