locked
Parsing a Query String on a console executable RRS feed

  • Question

  • I have a strange situation here. I have some legacy code that needs to be updated. It's for an internal web app that acts as a distribution page for our products. The pages have links that call a console app with a query string. The console app parses the query string to and serves up the requested product in zip or tar format. The original code was written in C++ but I'm rewriting it in C#. My question is, how can I parse that query string?

    Here's an example:

     <a href="../../scripts/myapp.exe?tcpip\A43\V2c-TAR"></a>

    The existing app will use this to get to the directory and "TAR" all the contents.

    The directory in this case is "tcpip\A43\V2c"

    The -TAR is telling the app to TAR the contents instead of zipping them.

    I need my C# console app to accept the query string as an input parameter.

    Thanks in advance

    Thursday, March 13, 2014 2:07 PM

Answers

  • Thank you for your answers, but I believe I need to pursue another method.

      

    Generally the only way for a app to get inforamtion during start is from command line parameters.

    Command line Parameters in C# can be accessed via the "string[] args" argument main can take:
    http://msdn.microsoft.com/en-us/library/acy3edy3.aspx

    How about you compile a programm that prints what it get's from the command line, set it so up that the above query fires that test-app and you tell us what gets to the programm. From there we can figure out how to interpret the data.

    Parsing command line argument is not an easy task. More so as the commandlien argument was effectively transmitted via http url (so space are not nessesary in there, where they would be with a normal command line argument).


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    • Marked as answer by Caillen Monday, March 24, 2014 2:28 AM
    Thursday, March 13, 2014 7:02 PM
  • "The link is used to launch an executable (something Microsoft has specifically tried to prevent in IIS). The executable needs to be able to parse what is sent in the link after the '?'. "

    This sounds like a CGI application. There are a bunch of environment variables that are passed by the server to CGI apps, see here for a example.

    If that's what you're looking for then you need to use Environment.GetEnvironmentVariable to retrieve the relevant values. For example PATH_TRANSLATED may be of some use.

    • Marked as answer by Caillen Monday, March 24, 2014 2:28 AM
    Thursday, March 13, 2014 7:18 PM

All replies

  • If your query is always a well-formed xml fragment, you could use an xml parser.

    http://msdn.microsoft.com/en-us/library/cakk7ha0(v=vs.110).aspx

    XmlTextReader tr = new XmlTextReader(
    @"<element1> abc </element1> 
      <element2> qrt </element2>
      <?pi asldfjsd ?>
      <!-- comment -->"
    , XmlNodeType.Element, null);
                while (tr.Read()) {
                    Console.WriteLine("NodeType: {0,-12} NodeName: {1,-12} {2}", tr.NodeType, tr.Name, tr.NodeType == XmlNodeType.Text ? tr.ReadContentAsString() : "");
                }

    For the contents of the href attribute, I'd use a regular expression, but without a formal specification or a global view it's hard to choose a well proportioned answer (regular expressions are very compact and powerful, but they're overkill for the simplest cases).


    if a problem looks too big, break it into smaller objects

    Thursday, March 13, 2014 2:48 PM
  • Uri can handle many URL formats so I'd start with it for parsing the href attribute.  If it is not a full URL then use the overload that allows you to specify partial.  Once you have it in a Uri you have access to the query string.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, March 13, 2014 2:54 PM
  • Not sure if I've made my question clear. This is a static HTML page. The link is used to launch an executable (something Microsoft has specifically tried to prevent in IIS). The executable needs to be able to parse what is sent in the link after the '?'.

    So in the example I used above. The user will click the link to "V2c". The application gets launch from that link and is passed the "Query String". In this case it would be "tcpip\A43\V2c". That is a path to the contents of V2c. The app then gets all the files from that directory, zip's or tar's them up, and sends it back to the requesting page.

    So if I were to use Uri, how can I get it to read the string at start up as an input parameter?

    EDIT: I've added the code that I have so far.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    
    
    namespace pbgetsharp
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                // get the query string used
                Console.WriteLine("argument count = {0}", args.Length);
                for (int i = 0; i < args.Length; i++)
                {
                    Console.WriteLine("Arg[{0}] is [{1}]", i, args[i]);
                }
    
                
    
    
    
                // this will get the IP4 ip address 
                string ip = "";
                string strHostName = "";
                strHostName = Dns.GetHostName();
                IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
                IPAddress[] addr = ipEntry.AddressList;
                ip = addr[1].ToString();
                Console.WriteLine("The IP address is {0}", ip);
            }
        }
    }

    • Edited by Jim Bedson Thursday, March 13, 2014 3:23 PM
    Thursday, March 13, 2014 3:17 PM
  • Ok so the issue is that your console app is getting the string you mentioned and you need to parse it.  So it doesn't really matter that this came from an HTML page or that it was part of a query string.  The question is how to parse the string from your app.  Based upon my understanding your string will be of the form <path>-<format>.  You get break the string up into its parts using split.

    var tokens = inputString.Split(new char[] { '-' }, 2);
    var path = tokens[0];
    var format = (tokens.Length == 2) ? tokens[1] : "";

    Now that you have the path you have to determine the rules for determining the root of the path.  By default the path is a relative path under the current page.  So if you had the HTML file in \abc then "tcpip\xyz" would have the path "\abc\tcpip\xyz".  If the path starts with a / then it is rooted to the web site directory.  But the issue is that these paths are relative to the server and not to the machine so there is no way to get the physical path without the web server actually getting involved.

    You're going to have to determine how to find the physical path for the string.  There is no way to do this automatically without making some assumptions such as where the physical path to the web site is or the full URL to the site itself.  A base web path by itself can reside anywhere on a machine.

    Thursday, March 13, 2014 4:53 PM
  • Thank you for your answers, but I believe I need to pursue another method.

      
    Thursday, March 13, 2014 6:54 PM
  • Thank you for your answers, but I believe I need to pursue another method.

      

    Generally the only way for a app to get inforamtion during start is from command line parameters.

    Command line Parameters in C# can be accessed via the "string[] args" argument main can take:
    http://msdn.microsoft.com/en-us/library/acy3edy3.aspx

    How about you compile a programm that prints what it get's from the command line, set it so up that the above query fires that test-app and you tell us what gets to the programm. From there we can figure out how to interpret the data.

    Parsing command line argument is not an easy task. More so as the commandlien argument was effectively transmitted via http url (so space are not nessesary in there, where they would be with a normal command line argument).


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    • Marked as answer by Caillen Monday, March 24, 2014 2:28 AM
    Thursday, March 13, 2014 7:02 PM
  • "The link is used to launch an executable (something Microsoft has specifically tried to prevent in IIS). The executable needs to be able to parse what is sent in the link after the '?'. "

    This sounds like a CGI application. There are a bunch of environment variables that are passed by the server to CGI apps, see here for a example.

    If that's what you're looking for then you need to use Environment.GetEnvironmentVariable to retrieve the relevant values. For example PATH_TRANSLATED may be of some use.

    • Marked as answer by Caillen Monday, March 24, 2014 2:28 AM
    Thursday, March 13, 2014 7:18 PM