access API on imagoxt - vb.net

i must download some information from API exposed by a server, but whit information in my hand i'm very in difficult.
my code :
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim address As Uri
Dim data As StringBuilder
Dim byteData() As Byte
Dim postStream As Stream = Nothing
address = New Uri("https://www.xxxxxxx.com/api/ajax/widget/refreshWidget")
request = DirectCast(WebRequest.Create(address), HttpWebRequest)
request.Method = "POST"
request.Headers("Authorization") = "Bearer " & "owvNGiOautorizzazoGDNHVGpSO2xcVzE8207JqnjNj"
request.Accept = "application/json"
request.ContentType = "multipart/form-data"
request.Host = "www.xxxxxxx.com"
request.ContentType = "application/x-www-form-urlencoded"
request.Headers.Add("userid", "238")
request.Headers.Add("usernodeid", "1036")
request.Headers.Add("dashboardid", "589")
request.Headers.Add("widgetid", "6699")
data = New StringBuilder()
'data.Append("type: " & HttpUtility.UrlEncode("datatable"))
data.Append(body) 'HttpUtility.UrlEncode(body))
byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
request.ContentLength = byteData.Length
Try
postStream = request.GetRequestStream()
postStream.Write(byteData, 0, byteData.Length)
Finally
If Not postStream Is Nothing Then postStream.Close()
End Try
Try
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Debug.Print(reader.ReadToEnd())
Finally
If Not response Is Nothing Then response.Close()
End Try
with this code i can access to API whit no error,
but i can't receive anythings.

Related

WebRequest send file

I am consuming an API and, in a POST, I need to send a file.
My function:
Public Sub SendRequest(uri As Uri, jsonDataBytes As Byte())
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
Dim request As WebRequest
request = WebRequest.Create(uri)
request.ContentLength = jsonDataBytes.Length
request.Method = "POST"
Dim auth = "Basic xxxxxxxxxxx"
request.Headers.Add("Authorization", auth)
request.ContentType = "application/json"
Using requestStream = request.GetRequestStream
requestStream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
requestStream.Close()
Try
Using responseStream = request.GetResponse.GetResponseStream
Using reader As New StreamReader(responseStream)
Dim objResponse As Object = reader.ReadToEnd()
End Using
End Using
Catch ex As WebException
End Try
End Using
End Sub
jsonDataBytes is:
Dim data = File.ReadAllBytes(filePath)
But, it does not work. Any help?

How To Send The Body Of Http Post Request using HTTPWebRequest

I just want to POST a body data in a x-www-form-urlencoded format in a specific URL. I searched a lot but I only found how to send a parameter in an HTTP request post in VB.NET.
This is my code, but when I see the output it is just sending the post parameter:
Try
Dim TextBox10 = "12637"
Dim request As WebRequest = WebRequest.Create("https://ptsv2.com/t/delh1-1545643919")
request.Method = "POST"
Dim postbody As String = "employee_id=" + TextBox1.Text + "&company_code=" + TextBox2.Text + "&reg_key=" + TextBox3.Text + ""
Dim requestBody As String = "FormFieldName=Hello+World!"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postbody)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()
Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)
dataStream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
TextBox4.Text = responseFromServer
Console.WriteLine(responseFromServer)
'Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
Catch ex As Exception
MsgBox("NO DATA FOUND / CHECK INTERNET CONNECTION")
TextBox4.Text = ""
TextBox3.Text = ""
TextBox2.Text = ""
End Try
This is the output of the posted code:
How should I send the body data?

PayPal Get Access Token vb.net The request was aborted: Could not create SSL/TLS secure channel

I have a few questions on calling Grant Access Token API of PayPal, below is my code:
Public Function testAPI() As String
Dim data As StringBuilder
Dim byteData() As Byte
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim postStream As Stream = Nothing
Dim reader As StreamReader
Dim Result As String = ""
request = DirectCast(WebRequest.Create("https://api.sandbox.paypal.com/v1/oauth2/token"), HttpWebRequest)
request.Headers.Add("Username", "XXXXXXXXXX")
request.Headers.Add("Password", "XXXXXXXXXX")
data = New StringBuilder("{""grant_type"":""client_credentials""}")
request.ContentType = "application/x-www-form-urlencoded"
request.Method = WebRequestMethods.Http.Post
byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
request.ContentLength = byteData.Length
postStream = request.GetRequestStream()
postStream.Write(byteData, 0, byteData.Length)
If Not postStream Is Nothing Then postStream.Close()
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Result = reader.ReadToEnd().ToString
Return Result
End Function
Am I passing Username and Password in right way?
Am I passing "grant_type":"client_credentials" in right way?
I keep getting error: The request was aborted: Could not create SSL/TLS secure channel. I have tried to add some code that I searched from google:
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
request.ProtocolVersion = HttpVersion.Version11
But it still failed and return same error message. Please help.

Context type error http post visual basic

I have been trying to send an HTTP post to a web service and got this message:
"The 'Content-Type' header must be modified using the appropiat
property or method, Parameter nama:name"
This happens when i try to update a customer with this JSON fotmat :
{"display_name":"john smith","email":"johnsmith#something","company_name":"enterprise"}
This is a part of the code i have been using to connect to the API:
Dim sUrl As String = "https://subscriptions.zoho.com/api/v1/customers"
Dim wHeader As WebHeaderCollection = New WebHeaderCollection()
Dim wRequest As HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(sUrl), HttpWebRequest)
wHeader.Clear()
wHeader.Add("Authorization: Zoho-authtoken 7ca5747efb3be868e155e707e679f9f5")
wHeader.Add("X-com-zoho-subscriptions-organizationid: 397080968")
wHeader.Add("Content-Type: application/json;charset=UTF-8")
wRequest.Accept = "application/json"
MsgBox(wHeader.ToString)
wRequest.Headers = wHeader
wRequest.Method = "POST"
Dim postData As String = sw.ToString
MsgBox(postData)
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
wRequest.ContentLength = byteArray.Length
Dim dataStream As Stream = wRequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = wRequest.GetResponse()
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
Console.WriteLine(responseFromServer)
LblResponse.Text = sResponse
Console.WriteLine(sResponse)
reader.Close()
dataStream.Close()
response.Close()
Catch ex As Exception
If ex.InnerException IsNot Nothing Then
LblResponse.Text = ex.Message + " >> " + ex.InnerException.ToString()
Else
LblResponse.Text = ex.Message
End If
End Try
any ideas ?

VB.Net 401 Unauthorized HTTP Web Request

I'm attempting to login to a cPanel using a POST Request in VB.Net. I have the correct credentials when logging in and when posting I still get an 'Unauthorized (401)' response when it should be '301' (analysed using Tamper Data Firefox Add-On). Below is my post request information and function.
Private Function POSTreq(ByVal URL$, ByVal Data$)
Dim tempCookie As New CookieContainer
Dim DataBytes As Byte() = Encoding.ASCII.GetBytes(Data)
Dim Request As HttpWebRequest = TryCast(WebRequest.Create(URL), HttpWebRequest)
Request.Method = "POST"
Request.ContentType = "application/x-www-form-urlencoded"
Request.ContentLength = DataBytes.Length
Dim PostData As Stream = Request.GetRequestStream()
PostData.Write(DataBytes, 0, DataBytes.Length)
PostData.Close()
Dim Response As HttpWebResponse = Request.GetResponse()
Dim ResponseStream As Stream = Response.GetResponseStream()
Dim StreamReader As New StreamReader(ResponseStream)
Dim Text$ = StreamReader.ReadToEnd()
Return Text
End Function
Post URL
http://example.com:2082/login/
Post Data
login_theme=cpanel&user=USERNAME&pass=PASSWORD&goto_uri=%2F
I could reproduce your described behaviour with your code.
If I set the CookieContainer it works on my side, and I was able to log in:
rem ...
Request.CookieContainer = tempCookie
Request.Method = "POST"
rem ... and so on ...
The second Solution would be to just provide the Credentials:
rem ...
Dim myFullUri = new Uri(URL)
Dim myCredentials As New NetworkCredential(Username, Password)
Dim myCache As New CredentialCache()
rem Add the credentials for that specific host and
rem for "Basic" authentication only
myCache.Add(New Uri(myFullUri.Scheme & "://" & myFullUri.Authority), _
"Basic", myCredentials)
Request.Credentials = myCache
Request.CookieContainer = tempCookie
Request.Method = "POST"
rem ... and so on ...