curl 命令到 html 或 vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14835916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:18:12  来源:igfitidea点击:

Curl command to html or vb.net

htmlvb.netapicurl

提问by need_the_buzz

I am trying to access the smartsheet API. They have a sample code provided in curl to access it.

我正在尝试访问 smartsheet API。他们在 curl 中提供了一个示例代码来访问它。

To access your Sheet list, construct an HTTPS request using your favorite programming or scripting language. Here is an example using the curl from a linux command line:

要访问您的工作表列表,请使用您喜欢的编程或脚本语言构建 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"

How do I do that in vb.net or from a html form?

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

回答by Ciarán

This is a rather large subject but at it's most simple you could try this...

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

Imports System.Net

and then...

进而...

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

I'm unfamiliar with the smartsheet API but you can use this as a start point.

我不熟悉 smartsheet API,但您可以将其用作起点。

If you are using a Proxy you will need to add...

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

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

and specify the proxy when you make the request...

并在您提出请求时指定代理...

wRequest.Proxy = wProxy

回答by sloth

To create a web request in VB.Net, you can use the HttpWebRequestclass.

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

The -Hargumentin curl creates an extra header. To add headers to a HttpWebRequestyou simply add them to the WebHeaderCollectionHeaders.

curl 中的-H参数创建了一个额外的标题。要将标题添加到 aHttpWebRequest您只需将它们添加到.WebHeaderCollectionHeaders

Example:

例子:

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)