SHELL Escape Command produces Error from Within Code but Not From Command Prompt

Rick Becker
Rick Becker used Ask the Experts™
on
Ok I have a Simple ( maybe not so simple ) SHELL escape that fails when executing from within my code but runs fine when executed from a Command Prompt. Here is the command that I am trying to execute:

 Shell("curl ""https://api.discogs.com/database/search?q=" + SearchString + """ -H " + """Authorization: Discogs token=" + MyToken + """ > C:\Downloads\CurlOutput.txt 2>&1")

Open in new window


I get a system error telling that the file NOT FOUND... I am redirecting the output TO a file NOT reading FROM a file???

Here is the expanded version of that string.. I can change my TOKEN at any time so it is ok...

curl "https://api.discogs.com/database/search?q=0075992511025" -H "Authorization: Discogs token=xyz" > C:\Downloads\CurlOutput.txt 2>&1

Open in new window


I am using VS 2017 and I am coding in VB.NET on a Windows 10 Machine

Thanks in advance
Rick
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I would do something like this instead:
Imports System.IO
Imports System.Net

Module Module1
    Sub Main()
        Dim search = "0075992511025"
        Dim token = "xyz"
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse
        Try
            request = HttpWebRequest.Create($"https://api.discogs.com/database/search?q={search}")
            With request
                .ContentType = "application/json"
                .Method = "GET"
                .Headers.Add("Authorization", $"Discogs token={token}")
                .UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
            End With
            response = CType(request.GetResponse(), HttpWebResponse)
            Using reader = New StreamReader(response.GetResponseStream())
                File.WriteAllText("C:\Downloads\CurlOutput.txt", reader.ReadToEnd())
            End Using
        Catch ex As Exception
            Console.WriteLine($"Exception while reading from {request.Address}{Environment.NewLine}{ex.Source} - {ex} [{ex.Message}]")
        End Try
        Console.ReadLine()
    End Sub
End Module

Open in new window

-saige-
Rick BeckerRetired-Sorta

Author

Commented:
Hi Saige,

Well I have been trying to use WEBCLIENT but I was also getting nowhere with that. I had tried 'POSTing' as well and also no luck...

This work Right Away so I thank you very much.. I will use your solution but I am still confused as to why the SHELL command does not work. That was a Single line solution vs a multi-line solution... The Great news is that one works while the other does not.... hehe

Thanks again
Rick

Commented:
For what it's worth, chances are that Shell didn't work because it couldn't find the curl executable. Since you are not specifying a path to it, you would need the curl.exe file to be in the same folder as your application, or within the common system path.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Rick BeckerRetired-Sorta

Author

Commented:
WOW Gonzo You Are RIGHT... How can I give you credit for that.. I am so pressed for time that I grabbed the first solution that would work...

Man I'm sorry that I was so quick to jump...

Rick

Commented:
Well, personally I would recommend it_saige's answer anyway. It's more efficient than invoking the shell to run cURL, AND more importantly, it eliminates a 3rd party dependency, which is always a good thing. Don't get me wrong - I love cURL as much as the next person (and I use the curl plugin in PHP all the time), but .NET framework has all the tools that you need already. There's not much sense in ignoring what you have immediately available.

I only commented because there are some other similar situations where it's helpful to call some kind of command-line application (where .NET doesn't already have the functionality).
Rick BeckerRetired-Sorta

Author

Commented:
Ok thanks for the advice... I know that the internet allows for anonymity and anybody can say and be anything they want I just want to say thanks because I can use all the 'Extra' help that can get because you see I am 70 and most of my knowledge is 'OLD' so it takes me awhile  learn and understand some of these newer languages... and it 'Bugs'  me when something worked before and now doesn't...

Hehe

Commented:
If you hover your mouse cursor over the profile photo, you'll see a popup that indicates the expert's overall level and their experience level within a particular topic zone. So for example, if you hover over it_saige's photo, you can see that the overall level is 38 (which is decently high), and that he/she has a fairly high level of experience with VB.Net (level 21 right now). That info can sometimes help you establish some confidence in the answers people give.

Granted, nobody's perfect, and even experienced people are capable of giving bad answers, but the higher the level, the lower the risk of getting a bad answer from that person.

Meanwhile, my overall level is 40, but I haven't really participated a lot in VB.Net questions because I'm usually participating more on C# .NET and PHP questions, but I'm pretty familiar with the .NET framework in general.

In any event, these are just little tips to help you when asking questions.
Rick BeckerRetired-Sorta

Author

Commented:
Thanks Gonzo that is all great advice and information. I appreciate it very much....

I wonder if it would be out of line for you to take a look at my 1 outstanding question ..."Converting VB6 to VB.NET - File upload via  MSXML2.ServerXMLHTTP results in error in VB.NET" I am really stumped and I have tried many variations with no 'Additional' luck... meaning that I can get further with it but still a failure...

Anyway thanks for your interest and help

Rick
Rick BeckerRetired-Sorta

Author

Commented:
Thanks Qlemo,

OK that sounds great... and no I did not know about the change ... and yes I have not been on EE for quite some time now, but I have been "Dragged Out Of Moth Balls".... LOL

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial