So my goal is to send a HttpRequest to api, it returns a 500 internal server error.
According to them, other companies are succesfully using this Api, so it should work in theory. I send the json example string I use to test if it works, they confirmed it is correct. So what could cause the Error?
Public Function SendStringToHttpServer(dataString As String, uriString As String, credentials As NetworkCredential) As String
Dim resultInfo As String
Dim request As HttpWebRequest = CType(WebRequest.Create(uriString), HttpWebRequest)
request.Credentials = credentials
request.Timeout = 6000
request.Method = "POST"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(dataString)
request.ContentType = "application/json"
request.ContentLength = byteArray.Length
Try
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
response.Close()
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Return "done"
End Function
Note: I saw there are some similiar threads, like this. But I use it exactly the same way, so it SHOULD work but doesn't -.-
(Beside the fact that i have strict on and therefore cast explicitly)
EDIT: About the authentification methods: This works definitly, since if I use wrong username or password in the credenitals I get a 403: authorized Error and not a 500.
Furthermore the read request works just fine. Read request is very similiar just a basic HttpRequest which only requires the url and credentials.
EDIT 2: So I read that it can be tied to HttpRequest automaticlly adding and "Expect:100-continue" to the header.I prevent this now
ServicePointManager.Expect100Continue = False
But it still gives a 500 Error.
Related
Currently, I am developing a web service which going to be called post JSON. It works fine and I got my records posted without any issue.
My issue is to display the response. I used UploadData to send .. Do I need to use download data to receive? What if I need to show in the response in MessageBox.
Note that I am expecting a response in JSON format as well. Let me know at least the concept ? First I guess, I need to receive the response and I will deserialize it.
Here is my current code. Working fine but I can't show the response.
Public Function postData(ByVal JsonBody As String) As Boolean
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim reqString() As Byte
Try
Dim APIusername As String = "XXXXX"
Dim APIPassword As String = "XXXXX"
webClient.Headers("content-type") = "application/json"
webClient.Credentials = New System.Net.NetworkCredential(APIusername, APIPassword)
reqString = Encoding.Default.GetBytes(JsonBody)
resByte = webClient.UploadData(Me.urlToPost, "post", reqString)
resString = Encoding.Default.GetString(resByte)
Console.WriteLine(resString)
' Here I need to show the responses
webClient.Dispose()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return False
End Function
I have used http request instead of web client and it works.
I have a json request in my vb.net desktop app project (.net framework 4.5.2). This codes working every time correcctly in my computerand i saw response json string every time.
But i tried run in other machines, not worked. Response is every time nothing in other machines. Any ideas on the reason for this?
Dim wcOrders As List(Of Woocomerce.Root)
Net.ServicePointManager.ServerCertificateValidationCallback = Function() True
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
Dim client = New RestClient("https://[??myhost??].com.tr/wp-json/wc/v3/orders?context=view")
client.Timeout = -1
Dim request = New RestRequest(Method.[GET])
request.AddHeader("Authorization", "Basic [MyAuth.Info]")
Dim response As IRestResponse = client.Execute(request)
wcOrders = JsonConvert.DeserializeObject(Of List(Of Woocomerce.Root))(response.Content)
If IsNothing(wcOrders) Then
MsgBox("not")
ElseIf wcOrders.Count = 0 Then
MsgBox("0")
MsgBox(response.Content)
Else
MsgBox(wcOrders.Count)
End If
I've one API in vb.net and i want to make HTTP request to another API from one of my methods. I've created an instance of WebClient class and added my headers but the problem is i could not add my json object as body into HTTP request. Here is my code.
Dim webc As New WebClient
webc.Headers.Add("Content-Type: application/json")
webc.Headers.Add("Authorization: " + testHeader)
webc.Headers("x-iyzi-rnd") = random_string
Dim url = "https://sandbox-api.iyzipay.com/"
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim html As Byte() = webc.DownloadData(url)
Dim utf As UTF8Encoding = New UTF8Encoding()
Dim response As String = utf.GetString(html)
I don't really understand the make-up of JSON so I am struggling to adapt other answers to suit my needs. I really hope someone can help? Specifically I want to get a list or array of "elevations" from the following JSON response in VB. I have managed to get the response into a string and parsed it but that is as far as I have got.
VB.Net:
Imports Newtonsoft.Json.Linq
Dim urlstring As String = urlstring1 & coordstring & urlstring2
Dim srequest As HttpWebRequest = DirectCast(WebRequest.Create(urlstring), HttpWebRequest)
Dim responsestring As String
'Execute http enquiry
Try
Dim sresponse As New StreamReader(srequest.GetResponse().GetResponseStream())
responsestring = sresponse.ReadToEnd()
sresponse.Close()
'MessageBox.Show(responsestring)
Catch ex As Exception
MessageBox.Show("Error getting elevations, check internet connection")
GoTo X
End Try
Dim ser As JObject = JObject.Parse(responsestring)
JSON:
{
"authenticationResultCode":"ValidCredentials",
"brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
"copyright":"Copyright © 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
"resourceSets":[
{
"estimatedTotal":1,
"resources":[
{
"__type":"ElevationData:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
"elevations":[1776,1775,1777,1776],
"zoomLevel":14
}
]
}
],
"statusCode":200,
"statusDescription":"OK",
"traceId":"8d57dbeb0bb94e7ca67fd25b4114f5c3"
}
Use something to help you visualize, like https://jsonformatter.curiousconcept.com/
Collapse all then as you expand write your path.
When you get to {} you type the name, when you get to [] you type number.
YourArray = ser("resourceSets")(0)("resources")(0)("elevations")
Adding CruleD's response to my code:
Imports Newtonsoft.Json.Linq
Dim urlstring As String = urlstring1 & coordstring & urlstring2
Dim srequest As HttpWebRequest = DirectCast(WebRequest.Create(urlstring), HttpWebRequest)
Dim responsestring As String
'Execute http enquiry
Try
Dim sresponse As New StreamReader(srequest.GetResponse().GetResponseStream())
responsestring = sresponse.ReadToEnd()
sresponse.Close()
Catch ex As Exception
MessageBox.Show("Error getting elevations, check internet connection")
GoTo X
End Try
Dim ser As JObject = JObject.Parse(responsestring)
For Each token As JToken In ser("resourceSets")(0)("resources")(0)("elevations")
'Do something here with each item in the list such as add to an array
'e.g. elevationarray(tokennumber) = token
'tokennumber = tokennumber + 1
Next
In a VB .net environment, I am making the following call, trying to implement the authorize step of an OAuth process to connect to an Accelo API (a time-entry and billing type of app). I'm trying to get an access token:
Dim jsonstring = "{'Content-Type':'Application/x-www-Form-urlencoded',
'authorization':'Basic MDBhM...GbG5oLlZB'}"
Dim data = Encoding.UTF8.GetBytes(jsonstring)
Dim result_post = SendRequest(New Uri("https://ourinfo.api.accelo.com/oauth2/v0/token"), data, "application/json", "POST")
with the function defined as this:
Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
Dim req As WebRequest = WebRequest.Create(uri)
req.ContentType = contentType
req.Method = method
req.ContentLength = jsonDataBytes.Length
Dim stream = req.GetRequestStream()
stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
'stream.Close()
Dim response = req.GetResponse().GetResponseStream()
Dim reader As New StreamReader(response)
Dim res = reader.ReadToEnd()
reader.Close()
response.Close()
Return res
End Function
and I keep receiving an error on this line:
Dim response = req.GetResponse().GetResponseStream()
Saying
"System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'"
It seems to me like a syntax error or something in the way my calling method is formed or the format of the parameters passed. I got the HTTP request code suggestion/format from here:
How to POST a JSON to a specific url using VB.NET?
and I'm using the Accelo API to set the content-type and authorization "basic" part where the string is encoded in Base 64. This is a service application so i'm supposed to be able to get the token in 1-step (no user confirmation is required). I already have a "token" from when I registered, but the API still indicates I should do this code. i'm following this:
https://api.accelo.com/docs/?_ga=2.218971609.1390377756.1568376911-2053161277.1565440093#service-applications
Can anyone tell me what exactly I'm doing wrong here? I'm confused and this is the first time I'm trying to implement OAuth.
There is some example code in the API documentation that looks like this:
POST /oauth2/v0/token HTTP/1.1
Host: planet-express.api.accelo.com
Content-Type: application/x-www-form-urlencoded
Authorization: Basic {client_credentials}
grant_type=client_credentials
scope=read(staff)
And I'm not sure the difference between the = and : syntax purposes. I was not able to search and find any answers to whether I'm calling everything correctly. Should I be passing the scope and grant_type in the JSON string, or setting it as a property on "req" object in the "SendRequest" function? I know that the grant_type is supposedly required but how do I set it?
What's the token I received initially when I registered, if I'm supposed to get a token this way?
I got it working with the help of a colleague. Apparently I was confusing header data and the json "data,"and I was not formatting the data correctly either. I should have been looking at the WebResponse too, not only the WebRequest. I changed my code to the following, which works now:
Sub SendRequestGetAccess()
Dim req As WebRequest = WebRequest.Create(uri)
req.Method = "POST"
req.Headers.Add("Authorization", "Basic " & "MDB....")
req.ContentType = "Application/x-www-Form-urlencoded"
req.ContentLength = jsonDataBytes.Length
Dim stream = req.GetRequestStream()
stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
stream.Close()
Dim response As WebResponse = req.GetResponse()
Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)
Dim dataStream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
Console.WriteLine(responseFromServer)
reader.Close()
dataStream.Close()
response.Close()
'Dim firstItem = jsonResult.Item("data").Item(0).Value(Of String)("token")
Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(responseFromServer)
Dim _itemvalue = j("itemkey")
End Sub
And I had to use dashes instead of colons in my json data:
Dim jsonstring = "grant=creds" & "&scope=read"
Dim data = Encoding.UTF8.GetBytes(jsonstring)
SendRequestGetAccess(New Uri("https...site.com/oauth2/v0/token"), data)