|
Was the extra 'c' a test for the observant?
|
|
|
|
|
|
|
I've been struggling to send this CURL request in VB.NET and hope someone can help..
Here is a link to the documentation of the exact request I'm trying to send.
This is the sample CURL request:
curl -u 'username:token' 'https://api.dev.name.com/v4/domains' -X POST -H 'Content-Type: application/json' --data '{"domain":{"domainName":"example.org"},"purchasePrice":12.99}'
Here is the code that I put together and tested:
Dim uri As New Uri("https://api.dev.name.com/v4/domains")
Dim myUsername As String = txtUsername.Text.Trim & ":" & txtApiKey.Text.Trim
Dim data As String = "{""domain"":{""domainName"":""example.org""},""purchasePrice"":12.99}"
Dim dataByte As Byte()
request = WebRequest.Create(uri)
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes(myUsername)))
request.ContentType = "application/json"
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
request.Method = "POST"
dataByte = Encoding.UTF8.GetBytes(data)
request.GetRequestStream.Write(dataByte, 0, dataByte.Length)
Dim myResp As HttpWebResponse
myResp = request.GetResponse()
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
WebResponse = myreader.ReadToEnd()
MsgBox(WebResponse)
However, the code above always returns a (400) Bad Request.
Here is another variation of pretty much the same code which I tried:
Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse
Dim myUsername As String = "username:token"
myReq = HttpWebRequest.Create("https://api.dev.name.com/v4/domains")
myReq.Method = "POST"
myReq.ContentType = "application/json"
myReq.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes(myUsername)))
Dim myData As String = "{""domain"":{""domainName"":""example.org""},""purchasePrice"":12.99}"
Dim dataBytes As Byte() = Encoding.UTF8.GetBytes(myData)
myReq.GetRequestStream.Write(dataBytes, 0, dataBytes.Length)
myResp = myReq.GetResponse()
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
WebResponse = myreader.ReadToEnd()
MsgBox(WebResponse)
But, as you can already guess, it also returns a 400 (Bad Request).
|
|
|
|
|
In your CURL command, you are using the '-u' option to provide the username and token as a basic authentication header. In your VB.NET code, you're attempting to encode the credentials manually. Make sure that 'myUsername' has the correct username and token values, and try using the Credentials property of the 'WebRequest' object instead of manually adding the header -
request.Credentials = New NetworkCredential(txtUsername.Text.Trim, txtApiKey.Text.Trim)
The JSON data you're sending in the CURL command needs to be properly serialized before sending it, we use Newtonsoft.Json, not sure if it will help in your situation, if so you can use it where you create an anonymous type to represent the JSON structure and then serializes it using 'JsonConvert.SerializeObject()' -
Imports Newtonsoft.Json
Dim domainData As New With {
.domain = New With {
.domainName = "example.org"
},
.purchasePrice = 12.99
}
Dim data As String = JsonConvert.SerializeObject(domainData)
You need to close the request stream before sending the request -
request.GetRequestStream.Close()
Instead of directly displaying returned responses in a message box, you can return the status codes and any error messages further by using -
Dim statusCode As Integer = CType(myResp, HttpWebResponse).StatusCode
Dim responseText As String = String.Empty
If statusCode = HttpStatusCode.OK Then
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
responseText = myreader.ReadToEnd()
End If
MsgBox(responseText)
I hope any of these suggestions help.
|
|
|
|
|
Hey Andre,
Thanks so much for your detailed reply and explanation! I imported Newtonsoft.json no problem and tried implementing your changes. On a positive note, I'm no longer getting a (400) Bad Request reply. However, I'm now getting a (401) Unauthorized response. I've double checked my login details and made sure they were correct. I also tried testing in both the OTE (operational test environment) and the LIVE environment because the details are slightly different for each, but received the same error on both.
Could this be due to how the credentials are now being passed?
Also, if I close "GetRequestStream" before sending the request then I get a message the connection was suddenly terminated when trying to send the request. However, if I put it directly after writing to the stream that issue goes away. Here is a look at my updated code:
Dim uri As New Uri("https://api.dev.name.com/v4/domains")
Dim domainData As New With {
.domain = New With {
.domainName = "example.org"
},
.purchasePrice = 12.99
}
Dim data As String = JsonConvert.SerializeObject(domainData)
Dim dataByte As Byte()
request = WebRequest.Create(uri)
request.Credentials = New NetworkCredential(txtUsername.Text.Trim, txtApiKey.Text.Trim)
request.ContentType = "application/json"
request.Method = "POST"
dataByte = Encoding.UTF8.GetBytes(data)
request.GetRequestStream.Write(dataByte, 0, dataByte.Length)
request.GetRequestStream.Close()
Dim myResp As HttpWebResponse
myResp = request.GetResponse()
Dim statusCode As Integer = CType(myResp, HttpWebResponse).StatusCode
Dim responseText As String = String.Empty
If statusCode = HttpStatusCode.OK Then
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
responseText = myreader.ReadToEnd()
End If
MsgBox(responseText)
Any suggestions? Thanks again!
|
|
|
|
|
Closing the stream - my bad, I added it prematurely. Please disregard my suggestion to close the request stream before sending the request or add it after the stream were read.
(401) Unauthorized response, it seems that your server did not accept the given credentials as valid. - There are a few things you can try.
Make sure that the API you are accessing is expecting basic authentication and that the credentials should be provided in the Authorization header. If the API expects a different authentication method, such as API keys or OAuth, you will need to adjust your authentication to accept the request as per their requirements.
Check that the API endpoint you are using (https://api.dev.name.com/v4/domains) is 100% correct.
To isolate or debug the issue, you could try using a different client, such as cURL or Postman, with the same credentials and endpoint. This might help determine if the problem lies with the code or if there might be an issue with the credentials or API itself.
Check your API documentation to make sure that you are following the correct authentication process and that there are no additional steps required.
Hope this points you in the right direction.
|
|
|
|
|
hey..me again..at least i keep the forum moving :P ...thanks for your help...i have another question sorry...
i have this code
<pre>Public Class SSVoiceRecordCtrl
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Dim lSamples, lRet, lBits, lChannels As Integer
Dim iBlockAlign As Short
Dim lBytesPerSec As Integer
Dim J As Integer = 1
Public Enum SoundFormats
Mono_6kbps_8_Bit
Mono_8kbps_8_Bit
Mono_11kbps_8_Bit
Mono_16kbps_8_Bit
Mono_22kbps_8_Bit
Mono_32kbps_8_Bit
Mono_44kbps_8_Bit
Mono_48kbps_8_Bit
Stereo_24kbps_16_Bit
Stereo_32kbps_16_Bit
Stereo_44kbps_16_Bit
Stereo_64kbps_16_Bit
Stereo_88kbps_16_Bit
Stereo_1288kbps_16_Bit
Stereo_176kbps_16_Bit
Stereo_192kbps_16_Bit
End Enum
Public Enum MyState
Idle
Recording
Paused
End Enum
Private xState As MyState
Public ReadOnly Property State() As MyState
Get
State = xState
End Get
End Property
Private _SoundFormat As SoundFormats
Public Property SoundFormat() As SoundFormats
Get
SoundFormat = _SoundFormat
End Get
Set(ByVal Value As SoundFormats)
_SoundFormat = Value
End Set
End Property
Private Sub GetSoundFormat()
If _SoundFormat = SoundFormats.Mono_6kbps_8_Bit Then
lSamples = 6000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_8kbps_8_Bit Then
lSamples = 8000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_11kbps_8_Bit Then
lSamples = 11025 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_16kbps_8_Bit Then
lSamples = 16000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_22kbps_8_Bit Then
lSamples = 22050 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_32kbps_8_Bit Then
lSamples = 32000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_44kbps_8_Bit Then
lSamples = 44100 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_48kbps_8_Bit Then
lSamples = 48000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Stereo_24kbps_16_Bit Then
lSamples = 6000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_32kbps_16_Bit Then
lSamples = 8000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_44kbps_16_Bit Then
lSamples = 11025 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_64kbps_16_Bit Then
lSamples = 16000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_88kbps_16_Bit Then
lSamples = 22050 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_1288kbps_16_Bit Then
lSamples = 32000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_176kbps_16_Bit Then
lSamples = 44100 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_192kbps_16_Bit Then
lSamples = 48000 : lBits = 16 : lChannels = 2
End If
iBlockAlign = lChannels * lBits / 8
lBytesPerSec = lSamples * iBlockAlign
End Sub
Private FName As String
Public Property FileName() As String
Get
FileName = FName
End Get
Set(ByVal Value As String)
FName = Value
End Set
End Property
Public Function StartRecord() As Boolean
Call GetSoundFormat()
On Error GoTo ER
If FName = "" Then GoTo ER
Dim i As Integer
i = mciSendString("open new type waveaudio alias capture", vbNullString, 0, 0)
I = mciSendString("set capture samplespersec " & lSamples & " channels " & lChannels & " bitspersample " & lBits & " alignment " & iBlockAlign & " bytespersec " & lBytesPerSec, vbNullString, 0, 0)
I = mciSendString("record capture", vbNullString, 0, 0)
xState = MyState.Recording
StartRecord = True
picStatus.Image = imgList.Images(1)
Timer1.Enabled = True
Exit Function
ER:
StartRecord = False
Dim SSVoiceRecordControlExec As New ArgumentException("File Name Not Specified.")
Throw SSVoiceRecordControlExec
End Function
Public Function StopRecord() As Boolean
On Error GoTo ER
If FName = "" Then GoTo ER
Dim i As Integer
I = mciSendString("save capture " & FName, vbNullString, 0, 0)
I = mciSendString("close capture", vbNullString, 0, 0)
xState = MyState.Idle
StopRecord = True
picStatus.Image = imgList.Images(0)
Timer1.Enabled = False
Exit Function
ER:
i = mciSendString("close capture", vbNullString, 0, 0)
StopRecord = False
Dim SSVoiceRecordControlExec As New ArgumentException("File Name Not Specified.")
Throw SSVoiceRecordControlExec
End Function
Public Sub CloseRecord()
Dim i As Integer
i = mciSendString("close capture", vbNullString, 0, 0)
xState = MyState.Idle
picStatus.Image = imgList.Images(0)
Timer1.Enabled = False
End Sub
Private Sub Class_Initialize_Renamed()
xState = MyState.Idle
End Sub
Private Sub Class_Terminate_Renamed()
StopRecord()
End Sub
Protected Overrides Sub Finalize()
Class_Terminate_Renamed()
MyBase.Finalize()
End Sub
Public Function PauseRecord() As Boolean
On Error GoTo ER
If FName = "" Then GoTo ER
Dim RS As String
Dim cb, I As Integer
RS = Space(128)
If xState = MyState.Paused Then
I = mciSendString("record capture", vbNullString, 0, 0)
xState = MyState.Recording
picStatus.Image = imgList.Images(1)
Timer1.Enabled = True
ElseIf xState = MyState.Recording Then
I = mciSendString("pause capture", vbNullString, 0, 0)
xState = MyState.Paused
picStatus.Image = imgList.Images(0)
Timer1.Enabled = False
End If
PauseRecord = True
Exit Function
ER:
PauseRecord = False
Dim SSVoiceRecordControlExec As New ArgumentException("File Name Not Specified.")
Throw SSVoiceRecordControlExec
End Function
Private Sub SSVoiceRecordCtrl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
picStatus.Image = imgList.Images(0)
End Sub
Private Sub picStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picStatus.DoubleClick
System.Diagnostics.Process.Start("SNDVOL32.EXE")
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If J = 1 Then
picStatus.Image = imgList.Images(J)
J += 1
Else
picStatus.Image = imgList.Images(J)
J = 1
End If
End Sub
End Class
i understand the code by i do not see where to change the sound source for ex. to line in...
they say this
Notes : A record class, which when turned
' : into an object lets you record sound
' : through mic, cd-line, etc... into a file
|
|
|
|
|
Try asking Suresh Sutha, it's his/her code.
|
|
|
|
|
Probably deprecated...i will try NAudio...is it good alternative?
|
|
|
|
|
according to the code...may i add more of these (24-bits)
lSamples = 32000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_176kbps_16_Bit Then
lSamples = 44100 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_192kbps_16_Bit Then
lSamples = 48000 : lBits = 16 : lChannels = 2
End If
i would like to have
ElseIf _SoundFormat = SoundFormats.Stereo_192kbps_24_Bit Then
lSamples = 48000 : lBits = 24 : lChannels = 2
|
|
|
|
|
Sorry, no idea. As I said you need to talk to the person who wrote the code.
|
|
|
|
|
Ok thanks ...i will try..i just would like to know how to select the input source device...i think is just what misses in my new project....now im trying to make a sound recording..but i would like to record mic,line in and if possible desktop sounds..
|
|
|
|
|
I took also a look onto the Code and I can't aslo not find a Property like requested by you.
For me the posted Code is incomplete and also not able to make any recording.
So you really have to ask the person who has written this code for a complete Code ... or you should look anywhere else for Code which matches to your requirement ...
|
|
|
|
|
hey guys...can anyone tell me how to chnage the highlight color for my listbox?
i tried some google snippets but no luck...
Thanks!!
|
|
|
|
|
|
hi..yes im using this code:
<pre> Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
If ListBox1.SelectedItem <> "" Then
e.DrawBackground()
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.Graphics.FillRectangle(Brushes.Red, e.Bounds)
End If
Using b As New SolidBrush(e.ForeColor)
e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(e.Index)), e.Font, b, e.Bounds)
End Using
e.DrawFocusRectangle()
End If
End Sub
but it seems that the items text are not appearing in the right color..only when i click them...
this is hard to explain...
|
|
|
|
|
I have just tried it with the MSDN code in the link I gave you and it seems to work - with one correction. The following block:
If ((e.State And DrawItemState.Selected) = DrawItemState.Selected) Then
e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds)
Else
is missing the parentheses around e.State And DrawItemState.Selected .
|
|
|
|
|
i changed the color to white and it shows only white rectangles but no text!!!..i wish i can put an image or video..sometimes one image worths more than a thousand words!!
|
|
|
|
|
OK..i went a bit foward..it is formating but when i load my files they do not appear in the listbox..i have to click the listbox to show the files loaded..
|
|
|
|
|
When i load the files into the listbox nothing shows but if i click where the items should be they appear with the formated style
|
|
|
|
|
i think is because im starting with an empty listbox...as i said...when i load the files they do not show..but..if i click where they were supposed to be they show with the different highlight color (red for ex)...
|
|
|
|
|
Here is a complete sample to do what you need:
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing
Module FormMain
Public Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New LbForm)
End Sub
End Module
Public Class LbForm
Inherits System.Windows.Forms.Form
Private Sub InitializeComponent()
ListBox1 = New System.Windows.Forms.ListBox()
TextBox1 = New System.Windows.Forms.TextBox()
CopyBtn = New System.Windows.Forms.Button()
SuspendLayout()
ListBox1.Name = "ListBox1"
ListBox1.Location = New System.Drawing.Point(33, 44)
ListBox1.Size = New System.Drawing.Size(200, 200)
ListBox1.TabIndex = 0
ListBox1.FormattingEnabled = True
ListBox1.ItemHeight = 16
ListBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple
ListBox1.Sorted = True
ListBox1.BorderStyle = BorderStyle.FixedSingle
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
ListBox1.ClearSelected()
TextBox1.Name = "TextBox1"
TextBox1.Location = New System.Drawing.Point(253, 44)
TextBox1.Size = New System.Drawing.Size(200, 200)
TextBox1.TabIndex = 2
TextBox1.Multiline = True
CopyBtn.Name = "CopyBtn"
CopyBtn.Location = New System.Drawing.Point(206, 272)
CopyBtn.Size = New System.Drawing.Size(75, 23)
CopyBtn.TabIndex = 1
CopyBtn.Text = "Copy"
CopyBtn.UseVisualStyleBackColor = True
AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
Font = New System.Drawing.Font("Calibri", 10.0)
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Name = "LbForm"
ClientSize = New System.Drawing.Size(486, 314)
Controls.Add(ListBox1)
Controls.Add(TextBox1)
Controls.Add(CopyBtn)
Text = "ListBox Sample"
ResumeLayout(False)
PerformLayout()
End Sub
Private WithEvents ListBox1 As ListBox
Private WithEvents TextBox1 As TextBox
Private WithEvents CopyBtn As Button
Public Sub New()
InitializeComponent()
GetFileNames()
End Sub
Private Sub GetFileNames()
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = ".\"
openFileDialog1.Filter = "VB.NET Files (*.vb)|*.vb|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.Multiselect = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
For Each fname in openFileDialog1.SafeFileNames
ListBox1.Items.Add(fname)
Next
End If
End Sub
Private Sub CopyBtn_Click(sender As Object, e As EventArgs) Handles CopyBtn.Click
Dim lblist As StringBuilder
lblist = New StringBuilder
For Each name As String In ListBox1.SelectedItems
lblist.AppendLine(name)
Next
ListBox1.ClearSelected()
TextBox1.Text = lblist.ToString()
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, _
ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem
If ((e.State And DrawItemState.Selected) = DrawItemState.Selected) Then
e.Graphics.FillRectangle(Brushes.Yellow, e.Bounds)
Else
e.Graphics.FillRectangle(Brushes.Beige, e.Bounds)
End If
e.Graphics.DrawRectangle(Pens.Blue, e.Bounds)
e.Graphics.DrawString(ListBox1.Items(e.Index), Me.Font, _
Brushes.Black, e.Bounds.X, e.Bounds.Y)
e.DrawFocusRectangle()
End Sub
Private Sub ListBox1_MeasureItem(ByVal sender As Object, _
ByVal e As MeasureItemEventArgs) Handles ListBox1.MeasureItem
e.ItemHeight += 5
End Sub
End Class
I took some of the code from the link to the MSDN documentation that I provided in an earlier message. The basic idea is that it gets a list of filenames and populates the ListBox with them. You can then test the selection of individual items, and how to process the resulting set.
|
|
|
|
|
~well...theres a lot of code here i do not need...i guess im gonna quit this highlight thing..i cannot make it work..Thanks anyway!
|
|
|
|
|
All you need to do is copy the code for the ListBox1_DrawItem subroutine, since that will work for any ListBox. But more importantly you should work on actually understanding the code, rather than just using Copy and Paste, as I already suggested.
|
|
|
|
|