Create SOAP request with vb.net - vb.net

I'm trying to call a webservice in vb.net and I have followed this thread: WebService Send SOAP request and received response using visual Basic
These are the steps that I have followed:
I have already add the reference to the project (http://webservice_url?wsdl).
I have written the soap request in an xml file by hand (I attach the xml file as image here )
I have load the xml file into an string variable and call to the webservice like that:
Dim strDocumentoSoap As String
Dim document As XDocument = XDocument.Load("C:\myFile.xml")
strDocumentoSoap = document.ToString
Dim servicioWeb As svc.v79jIntegracionPerfilSesion = New svc.v79jIntegracionPerfilSesion
Dim sbLogin As New System.Text.StringBuilder
sbLogin.Append(strDocumentoSoap)
Dim hwrequest As System.Net.HttpWebRequest
hwrequest = System.Net.WebRequest.Create("http://myServiceURL/realizarPeticionPublicacionOSBUS?WSDL")
hwrequest.Method = "POST"
hwrequest.ContentType = "text/xml; charset=utf-8"
Dim byteData As Byte() = System.Text.Encoding.UTF8.GetBytes(sbLogin.ToString())
hwrequest.ContentLength = byteData.Length
Dim writer As Stream = hwrequest.GetRequestStream
writer.Write(byteData, 0, byteData.Length)
writer.Close()
Dim Response As System.Net.HttpWebResponse
Response = hwrequest.GetResponse
Dim responseStatus As String = Response.StatusDescription.ToString
Dim DataStream As Stream
Dim Reader As StreamReader
DataStream = Response.GetResponseStream()
Reader = New StreamReader(DataStream)
Dim SD2Request As String = Reader.ReadToEnd()
'SHOW THE RESPONSE INTO MSGBOX
MsgBox(SD2Request)
This works perfectly, but I have read in the internet that this practice is not recommended (hand made XML for SOAP), because there are other useful tools in .net.
Can anyone explain me other options or libraries for creating a soap request in VB.NET without creating the soapenvelope by hand.
Thank you very much in advance!
Marga

Related

VB.NET File upload using WebApi: UnExpected end of MIME

Everything seems to be working except I get this below error in the WEBAPI:
Unexpected end of MIME multipart stream. MIME multipart message is not complete.
When I upload the same file from postman to the WEBAPI endpoint it works perfectly. Makes me think its a problem with the VB application.
BTW: Not skilled in VB (Inherited Application trying to make it work to upload large files)
Dim fileBytes As Byte() = Nothing
Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x")
Using binaryReader As New BinaryReader(fileUpload1.PostedFile.InputStream)
fileBytes = binaryReader.ReadBytes(binaryReader.BaseStream.Length)
binaryReader.Close()
End Using
Dim request As HttpWebRequest =
WebRequest.Create("http://mylocalhost/api/directory")
request.Method = "POST"
request.ContentType = "multipart/form-data; boundary = " + boundary
request.ContentLength = fileBytes.Length
request.Accept = "*/*"
request.AutomaticDecompression = DecompressionMethods.GZip
request.KeepAlive = True
Dim newStream As IO.Stream = request.GetRequestStream()
newStream.Write(fileBytes, 0, fileBytes.Length)
'newStream.Close()
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As IO.Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
Debug.WriteLine("File uploaded")
Return True````

VB POST request whit form-control values

I am having different problems when trying to make a POST request to my API, the API works perfectly tested in postman.
I would like to know if anyone sees that I am doing wrong, thank you very much
Dim url As String = "http://0.0.0.0/connect"
Dim dataToPassFromFormData As String = "client_id=1020&client_secret=FF29D58E&grant_type=password&username=admin&password=123"
Dim enc As UTF8Encoding
Dim postdatabytes As Byte()
Dim request As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)
enc = New System.Text.UTF8Encoding()
postdatabytes = enc.GetBytes(dataToPassFromFormData)
request.Method = "POST"
request.ContentLength = postdatabytes.Length
Using stream = request.GetRequestStream()
stream.Write(postdatabytes, 0, postdatabytes.Length)
End Using
Dim response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
Dim receiveStream As Stream = response.GetResponseStream()
Dim readStream As StreamReader = New StreamReader(receiveStream, Encoding.UTF8)
Dim us As user
us = JsonConvert.DeserializeObject(Of user)(readStream.ReadToEnd())
response.Close()
readStream.Close()
Return us.access_token
My api expects these parameters by body / form-data
client_id=1020
client_secret=FF29D58E
grant_type=password
username=admin
password=123

Visual Basic convert string to audio (WAV) file and play

I'm using a VB win form and need to play a wav from string but I don't know how to do it.
Dim URl As String ="http://localhost/main.php?command=readdata"
Dim request As HttpWebRequest = HttpWebRequest.Create(URl)
request.Proxy = Nothing
request.UserAgent = "Test"
Dim response As HttpWebResponse = request.GetResponse
Dim responseStream As System.IO.Stream = response.GetResponseStream
Dim Reader As New System.IO.StreamReader(responseStream)
Dim data As String = Reader.ReadToEnd
Reader.Close()
Dim AudioData As String = Hex2String(data)
'here i wont play and save this data
You don't want to read the data as a string, since it's not a string. WAV data is binary data. It's meaningless as a string, and if you encode it as a string, you may lose some data in the process. So, if you aren't converting it to a string, you don't need the StreamReader at all. Just create a new file as a stream, and then copy all the bytes from the one stream to the other:
Dim url As String ="http://localhost/main.php?command=readdata"
Dim request As HttpWebRequest = HttpWebRequest.Create(url)
request.Proxy = Nothing
request.UserAgent = "Test"
Dim response As HttpWebResponse = request.GetResponse()
Using responseStream As Stream = response.GetResponseStream()
Using fileStream As New New FileStream("MyFile.wav", FileMode.CreateNew)
responseStream.CopyTo(fileStream)
End Using
End Using

how to use API in Vb net

What is the best way to issue a http get in VB.net? I want to get the result of a request like
http://89.36.220.56/api.php?photo=urlphoto&point=numberlikes&socks=proxy
Photo = URL Photo
Point = Number
Socks = Proxy
Dim url = "http://89.36.220.56/api.php?photo=urlphoto&point=numberlikes&socks=proxy"
Dim request = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim response = request.GetResponse()
Dim receiveStream As Stream = response.GetResponseStream()
Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
Dim data = readStream.ReadToEnd()
response.Close()
readStream.Close()

Download contents of webpage with VB

I am using Visual Basic 2010 Express and have found a way to read a file:
Dim byter = My.Computer.FileSystem.ReadAllBytes("C:/Documents and Settings/textfile.txt")
Can I do something similar if I want to read the contents of a website?
Try something like:
Dim uri as New Uri("http://thewebsite")
Dim request as HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = WebRequestMethods.Http.Get
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim webpageContents As String = reader.ReadToEnd()
response.Close()