正在通过 WebClient 检索 Excel 文件 POST
Retrieving Excel File via WebClient POST
我正在尝试 post 一些数据到 API 其中 returns 一个 Excel 文件,我能够成功地使用 UploadString
,但我在 UploadData
调用中收到 403 Forbidden 错误。 UploadString
调用返回的字符串部分可读,但文件/变量中有一些古怪的字符/格式。我查看了 WebClient
上的 Download
方法,但它们似乎都没有参数来接受我需要包含的 post 正文数据。我通过邮递员获取文件没有问题。任何帮助是极大的赞赏。提前致谢!
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
尝试
client.UploadData(postUrl, "POST", Encoding.ASCII.GetBytes(postData)
我正在尝试 post 一些数据到 API 其中 returns 一个 Excel 文件,我能够成功地使用 UploadString
,但我在 UploadData
调用中收到 403 Forbidden 错误。 UploadString
调用返回的字符串部分可读,但文件/变量中有一些古怪的字符/格式。我查看了 WebClient
上的 Download
方法,但它们似乎都没有参数来接受我需要包含的 post 正文数据。我通过邮递员获取文件没有问题。任何帮助是极大的赞赏。提前致谢!
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
尝试
client.UploadData(postUrl, "POST", Encoding.ASCII.GetBytes(postData)