שתף באמצעות


VB.NET REST API call with Basic Authorization

Question

Thursday, December 28, 2017 9:54 PM

I'm having troubles getting my REST API call to work.  I'm new to VB and need help. 

I'm connecting to a URL and the vendor tells me that when I connect the first time, I'll get an unauthorized error then I need to submit another request to download a file.  The results are in JSON.  The end goal is to download a file from the URL.

Below is my code.

Dim origResponse As HttpWebResponse
        Dim objResponse As HttpWebResponse
        Dim origReader As StreamReader
        Dim objReader As StreamReader

        Dim origRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://api.orls.voxeo.net/files/exports/2017/10/31?"), HttpWebRequest)
        myRequest.Headers.Add("Authorization", "Basic c3NhX2RzdDpBc3AzY3RfMTIzNA==")
        origRequest.AllowAutoRedirect = False
        origRequest.Method = "GET"

        Try
            Dim Stream As Stream = origRequest.GetRequestStream()
            origResponse = DirectCast(origRequest.GetResponse(), HttpWebResponse)
        Catch ex As WebException
            MsgBox(ex.Status)
            Dim myRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://api.orls.voxeo.net/files/exports/2017/10/31/Export-production-202715_20171030.zip.gpg"), HttpWebRequest)
            myRequest.Headers.Add("Authorization", "Basic c3NhX2RzdDpBc3AzY3RfMTIzNA==")
            Try
                objResponse = DirectCast(myRequest.GetResponse(), HttpWebResponse)

            Catch ex2 As WebException
                objReader = New StreamReader(objResponse.GetResponseStream())
                MsgBox(objReader)
                MsgBox(ex2.Status)
            End Try
        End Try

JSON Results are below.

[
  {
    "name": "Export-production-202715_20171030.zip.gpg",
    "directory": false,
    "size": 5132580,
    "lastModifiedDate": "Tue Oct 31 01:01:21 UTC 2017"
  }
]

All replies (20)

Thursday, December 28, 2017 9:58 PM

https://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services

it is where you should post.


Friday, December 29, 2017 3:30 AM

Hi GaryGreemKC,

I test your code and modify in some places, and don't find unauthorized error.

Don't get the request stream, quite simply. GET requests don't usually have bodies (even though it's not technically prohibited by HTTP). but that's what calling GetRequestStream is for, providing body data for the request.

Given that you're trying to read from the stream, it looks to me like you actually want to get the response and read the response stream from that.

 Dim origResponse As HttpWebResponse
        Dim objResponse As HttpWebResponse
        Dim origReader As StreamReader
        Dim objReader As StreamReader

        Dim origRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://api.orls.voxeo.net/files/exports/2017/10/31?"), HttpWebRequest)
        origRequest.Headers.Add("Authorization", "Basic c3NhX2RzdDpBc3AzY3RfMTIzNA==")
        origRequest.AllowAutoRedirect = False
        origRequest.Method = "GET"
        Try
            origResponse = DirectCast(origRequest.GetResponse(), HttpWebResponse)
            Dim Stream As Stream = origResponse.GetResponseStream()
            Dim sr As New StreamReader(Stream, Encoding.GetEncoding("utf-8"))
            Dim str As String = sr.ReadToEnd()
            MessageBox.Show(str)
        Catch ex As WebException
            MsgBox(ex.Status)
            Dim myRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://api.orls.voxeo.net/files/exports/2017/10/31/Export-production-202715_20171030.zip.gpg"), HttpWebRequest)
            myRequest.Headers.Add("Authorization", "Basic c3NhX2RzdDpBc3AzY3RfMTIzNA==")
            Try
                objResponse = DirectCast(myRequest.GetResponse(), HttpWebResponse)

            Catch ex2 As WebException
                objReader = New StreamReader(objResponse.GetResponseStream())
                MsgBox(objReader.ReadToEnd())
                MsgBox(ex2.Status)
            End Try
        End Try

Best Regards,

Cherry

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Friday, December 29, 2017 1:55 PM

I made the updates you recommended but I'm still receiving an error.  As soon as the first statement in the Try executes, it fails.  I get the below error message.

+ ex {"The underlying connection was closed: An unexpected error occurred on a send."} System.Net.WebException


Friday, December 29, 2017 2:03 PM

https://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services

it is where you should post.

I'm curious, what has this question to do with webservices or ASP.Net

Maybe can the tenth DA924 tell that to us (or is it the 25th one). 

Success Cor


Friday, December 29, 2017 3:35 PM

... the vendor tells me that when I connect the first time, I'll get an unauthorized error then I need to submit another request to download a file.

That sounds odd but I guess that's how he has it set up.

Where did you get the credentials from?

Basic c3NhX2RzdDpBc3AzY3RfMTIzNA==

Did the vendor give that to you also? If so, my guess is that it's expired now.

Can you find out just how his API works? If you can get a valid URL for the JSON text then I'll do the rest and hand it back to you.

"A problem well stated is a problem half solved.” - Charles F. Kettering


Friday, December 29, 2017 3:58 PM

I'd appreciate the help.  The URL,https://api.orls.voxeo.net/files/exports/2017/10/31?, in my initial post is valid.  When I put the URL in a browser, it asks me for my username and password.

The credentials shown are the Username and Password in base64 and that's required per the vendor.  I've been playing around with the ZappySys Rest API task and I can get the file downloaded using the base64 credentials above but I'm trying to do the same thing in VB so we don't have to buy it.  When using the ZappySys, I have to call the Rest API twice to get the file downloaded.  The first ZappySys Rest API call returns the below.

Request

GET https 1.1 ==> /files/exports/2017/10/31?
Host: api.orls.voxeo.net

>>>> HEADERS <<<<<
User-Agent: ZappySysApp/1.0.2017.11201
Accept: */*
Cache-Control: no-cache
Authorization: Basic c3NhX2RzdDpBc3AzY3RfMTIzNA==
Host: api.orls.voxeo.net
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

Response

GET 1.1 200 OK==> OK

>>>> HEADERS <<<<<
Transfer-Encoding: chunked
Content-Type: application/json
Date: Fri, 29 Dec 2017 15:55:50 GMT
Set-Cookie: EXPORTS=S2; path=/

>>>> Cookies <<<<<
EXPORTS=S2

>>>> CONTENT <<<<<
[
  {
    "name": "Export-production-202715_20171030.zip.gpg",
    "directory": false,
    "size": 5132580,
    "lastModifiedDate": "Tue Oct 31 01:01:22 UTC 2017"
  }
]

Then I do a 2nd Rest API call with URL, https://api.orls.voxeo.net/files/exports/2017/10/31/Export-production-202715_20171030.zip.gpg, which is the exact path of the file to download after connected.  


Friday, December 29, 2017 4:10 PM

Gary,

I'm guessing here but would you try something please?

In a new project, just run this test:

Imports System.Net

Public Class Form1
    Private Sub _
        Form1_Load(sender As System.Object, _
                   e As System.EventArgs) _
                   Handles MyBase.Load

        Using wc As New WebClient
            Dim cred As New NetworkCredential("UserNameHere", "PasswordHere")
            Dim s As String = wc.DownloadString("URL HERE")

            Stop
        End Using

        Stop

    End Sub
End Class

Obviously you'll want to put your real information in but when the code gets to "Stop" (inside the Using block) you should be able to hover your mouse over "s". When you do, hopefully you'll see the text and not "Nothing".

Can you?

"A problem well stated is a problem half solved.” - Charles F. Kettering


Friday, December 29, 2017 4:28 PM

I get the following error when trying to execute the DownloadString command.

The underlying connection was closed: An unexpected error occurred on a send.


Friday, December 29, 2017 4:31 PM

I get the following error when trying to execute the DownloadString command.

The underlying connection was closed: An unexpected error occurred on a send.

I assume that you're talking to me? We don't all see this forum the same so please make it obvious.

*****

You might want to try encoding the Username and Password like you talked about earlier.

Once you try that, let me know please?

"A problem well stated is a problem half solved.” - Charles F. Kettering


Friday, December 29, 2017 4:56 PM

Frank,

I tried the following and got the same error as before.

       Using wc As New WebClient
            wc.Headers.Add("Authorization", "Basic c3NhX2RzdDpBc3AzY3RfMTIzNA==")
            Dim s As String = wc.DownloadString("https://api.orls.voxeo.net/files/exports/2017/10/31?")

            Stop
        End Using


Friday, December 29, 2017 5:02 PM

I thought you said earlier that you have a way to download the JSON string?

Did you maybe mean through that third-party thing that you're trying to avoid buying?

What is the information that you're ultimately trying to get? Can it be done some other way and/or from some other organization?

"A problem well stated is a problem half solved.” - Charles F. Kettering


Friday, December 29, 2017 5:18 PM

Frank,

I'm able to see the JSON string thru a browser after I enter the username and password and am able to download the file using the third-party tool.  Ultimately, I was trying to download the file using VB.NET instead of using the third-party tool.


Friday, December 29, 2017 5:24 PM | 1 vote

I'm able to see the JSON string thru a browser after I enter the username and password...

Then you can get it via VB Net - we just have to figure out how. ;-)

I don't want you to give me the Username or Password here because you'll be giving it to the world.

If you try your method (HTTP request) or a WebClient using credentials - using the same credentials - it won't log in?

"A problem well stated is a problem half solved.” - Charles F. Kettering


Friday, December 29, 2017 5:30 PM

Frank,

Correct.  I've tried using the same credentials as I log in with the browser and it won't log in.  I've also tried using the base64 encoded value.  Both times, I receive the same error.

Gary


Friday, December 29, 2017 5:32 PM

Frank,

Correct.  I've tried using the same credentials as I log in with the browser and it won't log in.  I've also tried using the base64 encoded value.  Both times, I receive the same error.

Gary

Is there maybe an API key that we're missing? That's pretty common.

If not, do they have a public website that explains how to use their API? I'll go have a look there if they do.

"A problem well stated is a problem half solved.” - Charles F. Kettering


Friday, December 29, 2017 6:17 PM

Frank,

I don't believe there is an API key.  Unfortunately, the vendor's website is pretty weak regarding how the API works.  Here's a link.  http://help.voxeo.com/go/help/icm

Gary


Friday, December 29, 2017 6:25 PM

https://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services

it is where you should post.

I'm curious, what has this question to do with webservices or ASP.Net

Maybe can the tenth DA924 tell that to us (or is it the 25th one). 

Success Cor

Your ignorance that a Web service, a Restful Web service,  is something else other than 'and other Web services' truly shows some kind of ignorance here on your part. Why is that you must act like Donald Trump with an ignorant tweet?


Friday, December 29, 2017 6:33 PM

I made the updates you recommended but I'm still receiving an error.  As soon as the first statement in the Try executes, it fails.  I get the below error message.

+ ex {"The underlying connection was closed: An unexpected error occurred on a send."} System.Net.WebException

I gave you the forum where you will get the help needed, which deals with Restful Web services. The horse can be lead to the water, but it's up to the horse to drink.


Friday, December 29, 2017 6:49 PM

Frank,

I don't believe there is an API key.  Unfortunately, the vendor's website is pretty weak regarding how the API works.  Here's a link.  http://help.voxeo.com/go/help/icm

Gary

I didn't get anything from that.

Sorry that I couldn't help with it.

"A problem well stated is a problem half solved.” - Charles F. Kettering


Saturday, December 30, 2017 5:45 AM

Frank,

I tried the following and got the same error as before.

       Using wc As New WebClient
            wc.Headers.Add("Authorization", "Basic c3NhX2RzdDpBc3AzY3RfMTIzNA==")
            Dim s As String = wc.DownloadString("https://api.orls.voxeo.net/files/exports/2017/10/31?")

            Stop
        End Using

Why don't you put a try/catch around the code, get the stack trace that contains the possible inner-exception message, and it may tell you why the connection was forcibly closed?

And you should post to a forum that deals with Restful API(s), which is another form of a Web service that follows Service Oriented Architecture principles. The forum was given to you, because the VB.NET forum is NOT that forum.

And if you think the VB forum is that forum, then no wonder you are having trouble.  Someone using C# knows better than this, once they have been told where to post.