Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to get a token from a REST API. For this I use HttpClient. However, when reviewing the url that is sent in the request, it is automatically changed and removes the s from the https, leaving it as http and additionally places :10001 to the URL

I am programming in VS 2015 and the code I use is the following:

Public Async Function WSconHTTP(User As String, Passw As String) As Task

        Dim client As HttpClient = New HttpClient()
        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls
        client.DefaultRequestHeaders.Accept.Clear()
        client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
        Dim request As HttpRequestMessage = New HttpRequestMessage(HttpMethod.Post, "https://URLWEBxxx/IntegrationAPI_2/api/login")
        Dim formData = New List(Of KeyValuePair(Of String, String))()
        formData.Add(New KeyValuePair(Of String, String)("username", User))
        formData.Add(New KeyValuePair(Of String, String)("password", Passw))
        request.Content = New FormUrlEncodedContent(formData)
        Dim response = Await client.SendAsync(request)
        MessageBox.Show("Code status:  " & response.StatusCode & vbCrLf & " Result: " & response.Content.ReadAsStringAsync().Result & vbCrLf , "Reply WS", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

    End Function



The response status code that the request gives me is: 404 and the result is:

"{""Message"":""No HTTP resource was found that matches the request URI "http://URLWEBxxx:10001/IntegrationAPI_2/api/login'.""}"

I appreciate the help you can give me.

What I have tried:

I have tried with other libraries such as restsharp but it has not worked for me
for this code disable this line

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls
Posted
Updated 12-Feb-23 17:26pm
Comments
Richard Deeming 18-Jan-23 5:52am    
https://URLWEBxxx/ is not a public website, so this is something running on your local machine, or on your local network.

Whatever that points to, it's redirecting you to a non-HTTPS site on port 10001, and the target of that redirection doesn't recognise the resource you're requesting.

You need to find out what is issuing the redirect, and why it's redirecting to an invalid URL. We can't do that for you - we don't have access to your computer or network.

From what I could gather on Google, it looks like the s is removed from HTTPS as it runs on localhost.

I also found that you need to enter the actual values for your login directly to submit the URI, something like -

Dim request As HttpRequestMessage = New HttpRequestMessage(HttpMethod.Post, "https://URLWEBxxx/IntegrationAPI_2/api/yourusernamehere/yourpwdhere")


See THIS link here with quite a brief explanation.

Hope this helps!
 
Share this answer
 
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls

Dim client = New RestClient("" & URLSERVICE & "")
Dim request = New RestRequest(Method.POST)
request.AddHeader("Content-Type", "application/json")

request.RequestFormat = DataFormat.Json
Dim response As RestResponse = client.Execute(request)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900