使用WebClient发布数据

时间:2015-05-08 20:37:46

标签: json vb.net curl webclient

我正在尝试使用WebClient设置更改Twitch频道的标题,游戏和延迟的功能。 Twitch的API文档提供了一个示例:

curl -H 'Accept: application/vnd.twitchtv.v3+json' -H 'Authorization: OAuth <access_token>' \
-d "channel[status]=Playing+cool+new+game!&channel[game]=Diablo&channel[delay]=0" \
-X PUT https://api.twitch.tv/kraken/channels/test_channel

我可以让标题部分正常工作,我只是不知道处理-d或post数据部分。我尝试过创建一个NameValueCollection并使用UploadValue。我也尝试过UploadString。

有人能指出我正确的方向吗?如何使用WebClient在VB.net中处理上述示例(特别是-d部分)?

除了显示导致未授权的新方法外:

    Dim address As String = "https://api.twitch.tv/kraken/channels/" & Channel.TrimStart("#")
    Dim data As String = "channel[status]=" & ChannelTitle & "&channel[game]=" & Game & "&channel[delay]=" & Delay
    Dim method As String = "PUT"
    Dim client As WebClient = New WebClient()
    client.Headers("Accept") = "application/vnd.twitchtv.v3+json"
    client.Headers("Authorization") = "OAuth " & AuthCode
    Dim reply As String = client.UploadString(address, method, data)

1 个答案:

答案 0 :(得分:0)

在我看来,Twitch希望您使用您的参数HTTP PUT字符串。你可以试试:

Dim address As String = "https://api.twitch.tv/kraken/channels/test_channel"
Dim data As String = "channel[status]=Playing+cool+new+game!&channel[game]=Diablo&channel[delay]=0" 
Dim method As String = "PUT" 
Dim client As WebClient = New WebClient()
Dim reply As String = client.UploadString(address, method, data)

我对curl不太熟悉,所以如果这不起作用,也许-d会以某种方式将字符串处理成JSON,在这种情况下,您可能会尝试再次使用NameValueCollection,但使用带ToString的UploadString发送它( )。

您是否尝试使用UploadString?结果是什么?