通过WebClient POST检索Excel文件

时间:2015-01-07 13:48:55

标签: .net excel webclient

我正在尝试将一些数据发布到返回Excel文件的API,并且我能够成功使用UploadString,但我在UploadData上收到403 Forbidden错误呼叫。从UploadString调用返回的字符串部分可读,但文件/变量中有一些时髦的字符/格式。我查看了Download上的WebClient方法,但似乎没有一个参数可以接受我需要包含的帖子正文数据。我通过Postman获取文件没有任何问题。任何帮助是极大的赞赏。提前谢谢!

Dim client As New Net.WebClient
Using client
        Dim cookie As String = "myCookie"
        Dim contentType As String = "application/x-www-form-urlencoded"
        Dim postData As String = "contentThatNeedsPosted"
        Dim postURL As String = "www.myposturl.com"
        client.UseDefaultCredentials = True
        client.Headers.Add(HttpRequestHeader.Cookie, cookie)
        client.Headers.Add(HttpRequestHeader.ContentType, contentType)

        Dim response1 = client.UploadString(postUrl, postData)
        Dim response = client.UploadData(postUrl, System.Text.Encoding.ASCII.GetBytes(postData))

        File.WriteAllBytes("c:\myfile.xls", System.Text.Encoding.UTF8.GetBytes(response1))
End Using

1 个答案:

答案 0 :(得分:0)

尝试

client.UploadData(postUrl, "POST", Encoding.ASCII.GetBytes(postData)