curl命令到html或vb.net

时间:2013-02-12 15:27:48

标签: html vb.net api curl

我正在尝试访问智能表API。他们有一个curl提供的示例代码来访问它。

要访问工作表列表,请使用您喜欢的编程或脚本语言构建HTTPS请求。以下是使用linux命令行中的curl的示例:

curl https://api.smartsheet.com/1.0/sheets \
-H "Authorization: Bearer 0da6cf0d-848c-4266-9b47-cd32a6151b1f" \
-H "Assume-User: john.doe%40smartsheet.com"

我如何在vb.net或html表单中执行此操作?

2 个答案:

答案 0 :(得分:6)

这是一个相当大的主题,但最简单的是你可以试试这个......

Imports System.Net

然后......

Dim wHeader As WebHeaderCollection = New WebHeaderCollection()

wHeader.Clear()
wHeader.Add("Authorization: Bearer 0da6cf0d-848c-4266-9b47-cd32a6151b1f")
wHeader.Add("Assume-User: john.doe%40smartsheet.com")

Dim sUrl As String = "https://api.smartsheet.com/1.0/sheets"

Dim wRequest As HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(sUrl), HttpWebRequest)

'wRequest.ContentType = "application/json" ' I don't know what your content type is
wRequest.Headers = wHeader
wRequest.Method = "GET"

Dim wResponse As HttpWebResponse = DirectCast(wRequest.GetResponse(), HttpWebResponse)

Dim sResponse As String = ""

Using srRead As New StreamReader(wResponse.GetResponseStream())
    sResponse = srRead.ReadToEnd()
End Using

我不熟悉智能表API,但您可以将其作为起点。

如果您使用的是代理,则需要添加...

Dim wProxy As IWebProxy = WebRequest.GetSystemWebProxy()
wProxy.Credentials = System.Net.CredentialCache.DefaultCredentials

并在发出请求时指定代理...

wRequest.Proxy = wProxy

答案 1 :(得分:2)

要在VB.Net中创建Web请求,您可以使用HttpWebRequest类。

curl中的-H argument会创建一个额外的标头。要将标题添加到HttpWebRequest,只需将其添加到WebHeaderCollection Headers

示例:

Dim myHttpWebRequest = CType(WebRequest.Create("https://api.smartsheet.com/1.0/sheets"), HttpWebRequest)
myHttpWebRequest.Headers.Add("Authorization: Bearer 0da6cf0d-848c-4266-9b47-cd32a6151b1f")
myHttpWebRequest.Headers.Add("Assume-User: john.doe%40smartsheet.com")
Dim myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)