请教vb使用inet控件post数据的方法
DimInFileAsString="s:\voicetest\1.flac"DimfsAsIO.FileStream=NewIO.FileStream(InFile,I...
Dim InFile As String = "s:\voicetest\1.flac"
Dim fs As IO.FileStream = New IO.FileStream(InFile, IO.FileMode.Open)
Dim voice() As Byte
ReDim voice(fs.Length)
fs.Read(voice, 0, voice.Length)
fs.Close()
Dim url As New Uri("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1")
Dim Request As Net.HttpWebRequest
Request = Net.HttpWebRequest.Create(url)
Request.Method = "POST"
Request.ContentType = "audio/x-flac; rate=16000"
Request.ContentLength = voice.Length
Using Ws As IO.Stream = Request.GetRequestStream()
Ws.Write(voice, 0, voice.Length)
End Using
Using Response As Net.HttpWebResponse = Request.GetResponse()
Using ResponseStream As IO.Stream = Response.GetResponseStream()
Using ReadStream As IO.StreamReader = New IO.StreamReader(ResponseStream, System.Text.Encoding.UTF8)
TextBox1.Text = ReadStream.ReadToEnd
End Using
End Using
End Using
上面是我写的VB.NET方法,但我不知道怎么转成VB6.0,下面是我写的VB6.0但不能用。。。
private sub ToPost()
FileOutPath = "s:\voicetest\1.flac"
Dim MData() As Byte
ReDim MData(FileLen(FileOutPath) - 1)
Open FileOutPath For Binary As #1
Get #1, , MData
Close #1
Dim MUrl As String: MUrl = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1"
Dim MHreat As String: MHreat = "Content-Type: audio/x-flac; rate=16000;ContentLength :" + CStr(UBound(MData))
Inet1.Execute MUrl, "POST", MData, MHreat
end sub
Private Sub Inet1_StateChanged(ByVal State As Integer)
Select Case State
Case 12
Dim vtData As Variant '数据变量。
Dim strData As String: strData = ""
Dim bDone As Boolean: bDone = False
'取得第一块。
vtData = Inet1.GetChunk(1024, icString)
DoEvents
Do While Not bDone
strData = strData & vtData
DoEvents
'取得下一块。
vtData = Inet1.GetChunk(1024, icString)
If Len(vtData) = 0 Then
bDone = True
End If
Loop
MsgBox strData
End Select
End Sub
'VB6.0调试情况,state只出现过一次是8,然后就没有反应了。。。没有12 展开
Dim fs As IO.FileStream = New IO.FileStream(InFile, IO.FileMode.Open)
Dim voice() As Byte
ReDim voice(fs.Length)
fs.Read(voice, 0, voice.Length)
fs.Close()
Dim url As New Uri("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1")
Dim Request As Net.HttpWebRequest
Request = Net.HttpWebRequest.Create(url)
Request.Method = "POST"
Request.ContentType = "audio/x-flac; rate=16000"
Request.ContentLength = voice.Length
Using Ws As IO.Stream = Request.GetRequestStream()
Ws.Write(voice, 0, voice.Length)
End Using
Using Response As Net.HttpWebResponse = Request.GetResponse()
Using ResponseStream As IO.Stream = Response.GetResponseStream()
Using ReadStream As IO.StreamReader = New IO.StreamReader(ResponseStream, System.Text.Encoding.UTF8)
TextBox1.Text = ReadStream.ReadToEnd
End Using
End Using
End Using
上面是我写的VB.NET方法,但我不知道怎么转成VB6.0,下面是我写的VB6.0但不能用。。。
private sub ToPost()
FileOutPath = "s:\voicetest\1.flac"
Dim MData() As Byte
ReDim MData(FileLen(FileOutPath) - 1)
Open FileOutPath For Binary As #1
Get #1, , MData
Close #1
Dim MUrl As String: MUrl = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1"
Dim MHreat As String: MHreat = "Content-Type: audio/x-flac; rate=16000;ContentLength :" + CStr(UBound(MData))
Inet1.Execute MUrl, "POST", MData, MHreat
end sub
Private Sub Inet1_StateChanged(ByVal State As Integer)
Select Case State
Case 12
Dim vtData As Variant '数据变量。
Dim strData As String: strData = ""
Dim bDone As Boolean: bDone = False
'取得第一块。
vtData = Inet1.GetChunk(1024, icString)
DoEvents
Do While Not bDone
strData = strData & vtData
DoEvents
'取得下一块。
vtData = Inet1.GetChunk(1024, icString)
If Len(vtData) = 0 Then
bDone = True
End If
Loop
MsgBox strData
End Select
End Sub
'VB6.0调试情况,state只出现过一次是8,然后就没有反应了。。。没有12 展开
展开全部
何必如此复杂?
Private Sub Command1_Click()
Dim myurl As String, send_data As String, myhead As String
myurl = "网址"
send_data = "数据"
my_head = "Content-Type: application/x-www-form-urlencoded"
Inet1.Execute myurl, "POST", send_data, myhead
End Sub
更多追问追答
追问
问题是我post的是一个音乐文件的byte数组,然后还要读取返回值
上面的那个谷歌语音识别API,post一个flac文件,然后返回识别结果。。。
你的回复我不知道在那里读取识别结果
追答
漏了
还要
do while inet1.stillExecuting
doEvents
loop
之后就在要inet的changed事件里面判断 state = 12,来处理返回的数据
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询