将cURL命令转换为.NET

时间:2013-11-21 22:41:03

标签: .net curl

您有以下cURL命令,我想将其转换为VB.NET或C#。

curl https://api.box.com/2.0/users \-H "Authorization: Bearer ACCESS_TOKEN" \ -d '{"login": "eddard@company.com", "name": "Ned Stark"}' \ -X POST

我在VB.NET中尝试了以下内容:

Dim url As String = "https://api.box.com/2.0/users"
Dim wrq = CType(Net.WebRequest.Create(url), HttpWebRequest)
Dim postString As String = "login=eddard@company.com&name=Ned Stark"

wrq.Headers.Add("Authorization: Bearer " & o2ses.AccessToken)
wrq.Method = "POST"
wrq.ContentType = "application/x-www-form-urlencoded"
wrq.ContentLength = postString.Length

Dim wrqWriter As New StreamWriter(wrq.GetRequestStream())
wrqWriter.Write(postString)
wrqWriter.Close()

Dim responseReader As New StreamReader(wrq.GetResponse().GetResponseStream())
Dim responseData As String = responseReader.ReadToEnd()

我是cURL的新手,并且在.NET中使用HttpWebRequest,但之前已经将cURL命令转换为.NET(感谢这个网站:)),但是没有使用-d选项,所以我很善良丢失的当执行以下行时,它会一直返回错误400(错误请求)。

Dim responseReader As New StreamReader(wrq.GetResponse().GetResponseStream())

我认为这与我发送的数据有关(即它没有获得所需的正确“登录”和“名称”参数)。

感谢任何帮助。如果需要,我可以尝试提供更多信息。如上所述,我是一个新手,所以我会尽我所能。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道vb.net,但我认为授权标题应如下所示:

wrq.Headers.Add(HttpRequestHeader.Authorization, "Bearer " & o2ses.AccessToken);
相关问题