I am trying to get a bearer token from an identity server using the WebRequest Class because the program has to work with .NET 2.0, and the port from where I am getting the token is 10000.
I tried creating the WebRequest like
but neither one works. The first returns an "Unkown URL Prefix"-Error and the second one a "400 Bad Request"-Error.
Is there any other way to get the token under .NET 2.0 ?
Thank you very much in advance for your help.
-Simon
Edit:
Using wc As New WebClient()
Dim postData As String = "grant_type=" + sTokenGrantType + "&username=" + sIdentityServerClientName + "&password=" + sIdentityServerClientSecret + "&scope=Api"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
Dim responseArray As Byte()
wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded")
wc.Headers.Add(HttpRequestHeader.ContentLength, byteArray.Length.ToString)
wc.Headers.Add(HttpRequestHeader.UserAgent, "User-Agent: PostmanRuntime/7.15.0")
wc.BaseAddress = sIdentityServerURL
responseArray = wc.UploadData("/getToken", "POST", byteArray)
MsgBox(responseArray)
End Using
I tried using the WebClient class but that results in the following error: "An exception occurred during a WebClient request."
SOLVED (see below)
Thanks for all your help, but I solved it myself, by using the WebClient instead of a WebRequest. This is how I did it:
The code is slightly different than the one posted above. The exception I got with the WebClient occured because of an issue with one of the HTTP-Headers, that I set.
Using wc As New WebClient()
Dim postData As String = "grant_type=" + sTokenGrantType + "&client_id=" + sIdentityServerClientName + "&client_secret=" + sIdentityServerClientSecret + "&scope=Api"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
Dim responseArray As Byte()
wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded")
wc.BaseAddress = sIdentityServerURL
responseArray = wc.UploadData("/getToken", "POST", byteArray)
End Using