VB.NET中实现卷曲请求的等效方法是什么?
- 内容介绍
- 文章标签
- 相关推荐
本文共计795个文字,预计阅读时间需要4分钟。
在Visual Studio中使用Visual Basic执行上述请求的操作,可以通过以下步骤实现:
1. 添加WebClient组件: 在Visual Studio中,首先需要在你的项目中添加一个WebClient组件。这可以通过工具箱来完成,或者使用以下代码在代码编辑器中直接添加:
vb Dim client As New WebClient()
2. 设置请求的URL和认证信息: 将请求的URL和认证信息设置到WebClient中。
vb client.BaseAddress=URL client.Credentials=New NetworkCredential(username, password)
3. 设置请求方法: 设置请求方法为POST,并指定请求内容。
vb Dim data As String=Your data here client.UploadString(URL, POST, data)
4. 执行请求并获取响应: 执行请求并获取响应。
vb Try Dim response As String=client.UploadString(URL, POST, data) ' 处理响应 Catch ex As Exception ' 处理异常 End Try
5. 完整的示例代码:
vb Dim client As New WebClient() client.BaseAddress=URL client.Credentials=New NetworkCredential(username, password)
Dim data As String=Your data here Try Dim response As String=client.UploadString(URL, POST, data) ' 处理响应 Catch ex As Exception ' 处理异常 End Try
这段代码将执行一个POST请求到指定的URL,使用提供的用户名和密码进行认证,并上传一些数据。请根据实际情况替换URL、用户名、密码和数据。
实际上,我正在尝试使用Watson服务,该服务向URL发出请求,并且cURL代码如下所示.如何在Visual Studio中使用Visual Basic语言执行此请求的等效操作?curl -X POST -u "{username}":"{password}" —-header "Content-Type:application/json" --data "{\"input\": {\"text\": \"Turn on the lights\"}, \"context\": {\"conversation_id\": \"1b7b67c0-90ed-45dc-8508-9488bc483d5b\", \"system\": {\"dialog_stack\": [\"root\"], \"dialog_turn_counter\": 1, \"dialog_request_counter\": 1}}}" "gateway.watsonplatform.net/conversation/api/v1/workspaces/25dfa8a0-0263-471b-8980-317e68c30488/message?version=2016-09-20"
有关更多信息,请参阅文档的URL以及解决方案cURL:
www.ibm.com/watson/developercloud/conversation/api/v1/
凭证和其他一切都很好.我把它从我的Node.js示例中删除了:
var watson = require('watson-developer-cloud'); var conversation = watson.conversation({ username: '1793094a-e543-4e3a-891d-4b619f21271d', password: 'xjmacpjHceRj', version: 'v1', version_date: '2016-09-20' }); // Replace with the context obtained from the initial request var context = {}; conversation.message({ workspace_id: '7c7b099b-aed4-4d27-a379-8b2f33644600', input: {'text': 'Turn on the lights'}, context: context }, function(err, response) { if (err) console.log('error:', err); else console.log(JSON.stringify(response, null, 2)); });
VB.NET实现.
Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse Dim reader As StreamReader Try myReq = HttpWebRequest.Create("gateway.watsonplatform.net/conversation/api/v1/workspaces/7c7b099b-aed4-4d27-a379-8b2f33644600/message?version=2016-09-20") myReq.Method = "POST" myReq.ContentType = "application/json" myReq.Headers.Add("Authorization", Convert.ToBase64String(Encoding.Default.GetBytes("1793094a-e543-4e3a-891d-4b619f21271d:xjmacpjHceRj"))) Dim myData As String = "{input: {text: " + txtEnviar.Text + "}, context: {conversation_id: 1b7b67c0-90ed-45Dc-8508-9488bc483d5b\, system\: {dialog_stack: [root], dialog_turn_counter: 1, dialog_request_counter: 1}}}" myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd() txtMuestra.Items.Add(myText) Catch ex As Exception txtMuestra.Items.Add(ex) End Try End Sub
但是我收到了身份验证错误.我想发送身份验证的方法未经头部授权是不正确的.我不是VB.NET的用户,所以这就是为什么我有我的并发症.
Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse myReq = HttpWebRequest.Create("meineURI.net") myReq.Method = "POST" myReq.ContentType = "application/json" myReq.Headers.add("Authorization", "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"))) Dim myData As String = "yourDataHere" myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd
本文共计795个文字,预计阅读时间需要4分钟。
在Visual Studio中使用Visual Basic执行上述请求的操作,可以通过以下步骤实现:
1. 添加WebClient组件: 在Visual Studio中,首先需要在你的项目中添加一个WebClient组件。这可以通过工具箱来完成,或者使用以下代码在代码编辑器中直接添加:
vb Dim client As New WebClient()
2. 设置请求的URL和认证信息: 将请求的URL和认证信息设置到WebClient中。
vb client.BaseAddress=URL client.Credentials=New NetworkCredential(username, password)
3. 设置请求方法: 设置请求方法为POST,并指定请求内容。
vb Dim data As String=Your data here client.UploadString(URL, POST, data)
4. 执行请求并获取响应: 执行请求并获取响应。
vb Try Dim response As String=client.UploadString(URL, POST, data) ' 处理响应 Catch ex As Exception ' 处理异常 End Try
5. 完整的示例代码:
vb Dim client As New WebClient() client.BaseAddress=URL client.Credentials=New NetworkCredential(username, password)
Dim data As String=Your data here Try Dim response As String=client.UploadString(URL, POST, data) ' 处理响应 Catch ex As Exception ' 处理异常 End Try
这段代码将执行一个POST请求到指定的URL,使用提供的用户名和密码进行认证,并上传一些数据。请根据实际情况替换URL、用户名、密码和数据。
实际上,我正在尝试使用Watson服务,该服务向URL发出请求,并且cURL代码如下所示.如何在Visual Studio中使用Visual Basic语言执行此请求的等效操作?curl -X POST -u "{username}":"{password}" —-header "Content-Type:application/json" --data "{\"input\": {\"text\": \"Turn on the lights\"}, \"context\": {\"conversation_id\": \"1b7b67c0-90ed-45dc-8508-9488bc483d5b\", \"system\": {\"dialog_stack\": [\"root\"], \"dialog_turn_counter\": 1, \"dialog_request_counter\": 1}}}" "gateway.watsonplatform.net/conversation/api/v1/workspaces/25dfa8a0-0263-471b-8980-317e68c30488/message?version=2016-09-20"
有关更多信息,请参阅文档的URL以及解决方案cURL:
www.ibm.com/watson/developercloud/conversation/api/v1/
凭证和其他一切都很好.我把它从我的Node.js示例中删除了:
var watson = require('watson-developer-cloud'); var conversation = watson.conversation({ username: '1793094a-e543-4e3a-891d-4b619f21271d', password: 'xjmacpjHceRj', version: 'v1', version_date: '2016-09-20' }); // Replace with the context obtained from the initial request var context = {}; conversation.message({ workspace_id: '7c7b099b-aed4-4d27-a379-8b2f33644600', input: {'text': 'Turn on the lights'}, context: context }, function(err, response) { if (err) console.log('error:', err); else console.log(JSON.stringify(response, null, 2)); });
VB.NET实现.
Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse Dim reader As StreamReader Try myReq = HttpWebRequest.Create("gateway.watsonplatform.net/conversation/api/v1/workspaces/7c7b099b-aed4-4d27-a379-8b2f33644600/message?version=2016-09-20") myReq.Method = "POST" myReq.ContentType = "application/json" myReq.Headers.Add("Authorization", Convert.ToBase64String(Encoding.Default.GetBytes("1793094a-e543-4e3a-891d-4b619f21271d:xjmacpjHceRj"))) Dim myData As String = "{input: {text: " + txtEnviar.Text + "}, context: {conversation_id: 1b7b67c0-90ed-45Dc-8508-9488bc483d5b\, system\: {dialog_stack: [root], dialog_turn_counter: 1, dialog_request_counter: 1}}}" myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd() txtMuestra.Items.Add(myText) Catch ex As Exception txtMuestra.Items.Add(ex) End Try End Sub
但是我收到了身份验证错误.我想发送身份验证的方法未经头部授权是不正确的.我不是VB.NET的用户,所以这就是为什么我有我的并发症.
Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse myReq = HttpWebRequest.Create("meineURI.net") myReq.Method = "POST" myReq.ContentType = "application/json" myReq.Headers.add("Authorization", "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"))) Dim myData As String = "yourDataHere" myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd

