VB.NET中的卷曲请求等效 [英] Curl request equivalent in VB.NET

查看:83
本文介绍了VB.NET中的卷曲请求等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我正在尝试使用Watson服务,该服务向URL发出发布请求,而cURL代码如下。我该如何使用Visual Basic语言在Visual Studio中完成此请求?

  curl -X POST -u {用户名}: {密码}-标题 Content-Type:application / json --data {\ input\:{\ text\:\开灯\},\ context\:{\ conversation_id\:\ 1b7b67c0-90ed-45dc-8508-9488bc483d5b\,\ system\:{\ dialog_stack\:[\ root\],\ dialog_turn_counter\:1,\ dialog_request_counter\:1}}} https://gateway.watsonplatform.net/conversation / api / v1 / workspaces / 25dfa8a0-0263-471b-8980-317e68c30488 / message?version = 2016-09-20 

有关更多信息,该文档的URL以及解决方案cURL:



解决方案

 将myReq作为HttpWebRequest 
将myResp作为HttpWebResponse

myReq = HttpWebRequest .Create( https://meineURI.net)

myReq.Method = POST
myReq.ContentType = application / json
myReq.Headers.add (授权,基本和& Convert.ToBase64String(Encoding.UTF8.GetBytes(用户名:密码)))
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
将myreader作为新的System.IO .StreamReader(myResp.GetResponseStream)
将myText作为字符串
myText = myreader.ReadToEnd


As they are, I am trying to use the Watson service, which makes a post request to a URL, and the cURL code is as follows. How could I do the equivalent of this request in Visual Studio with the Visual Basic language?

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}}}" "https://gateway.watsonplatform.net/conversation/api/v1/workspaces/25dfa8a0-0263-471b-8980-317e68c30488/message?version=2016-09-20"

For more information, the URL of the documentation, with the solution cURL:

https://www.ibm.com/watson/developercloud/conversation/api/v1/

The credentials and everything else is fine. I took it out of my example of 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 implementation.

    Dim myReq As HttpWebRequest
    Dim myResp As HttpWebResponse
    Dim reader As StreamReader

    Try
       myReq = HttpWebRequest.Create("https://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

But I get an authentication error. I guess the method of sending the authentication Not authorized by the headers is not quite correct. I am not a user of VB.NET so that is why I have my complications.

解决方案

Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse

myReq = HttpWebRequest.Create("https://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

这篇关于VB.NET中的卷曲请求等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆