Search results for 'GetEnvironmentVariable'. 1 post(s) found.

  1. 2007/08/27 How to send binary data through C# CGI app?
2007/08/27 13:05

How to send binary data through C# CGI app?


Following example implemented binary data downloading.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace cscgi
{
  class CsCGI
  {
    private static string dbgString = "id=1234&pic=123.gif";
    private List<string> lstKeys = new List<string>();
    private List<string> lstVals = new List<string>();
    static void Main(string[] args)
    {
      CsCGI app = new CsCGI();
      ParseParams(app);
      string sID = GetParam(app, "id");
      string sFile = GetParam(app, "pic");
      try
      {
        FileStream fs = new FileStream(sFile, FileMode.Open);
        BinaryReader brFile = new BinaryReader(fs, Encoding.UTF8);
        byte[] bBuff = new byte[fs.Length];
        brFile.Read(bBuff, 0, (int)fs.Length);
        string sBuff = Encoding.ASCII.GetString(bBuff);
        // if it's just text file, change binary/plain to text/html
        Console.Write("Content-Type: binary/plain\n");
        Console.Write("Content-Length: " + fs.Length.ToString());
        Console.Write("\n\n");
        Console.Write(sBuff);
      }
      catch (Exception e)
      {
        Console.Write(e.Message + "\n");
      }
      finally
      {
        //
      }
    }

    private static void ParseParams(CsCGI app)
    {
      //string queryString = Environment.GetEnvironmentVariable("QUERY_STRING");
      string queryString = dbgString;
      string[] sParams = queryString.Split('&');
      string[] sParamsSeperated;
      for (int i = 0; i < sParams.Length; i++)
      {
        sParamsSeperated = sParamsIdea.Split('=');
        app.lstKeys.Add(sParamsSeperated[0]);
        app.lstVals.Add(sParamsSeperated[1]);
      }
    }

    private static string GetParam(CsCGI app, string key)
    {
      int idx = app.lstKeys.BinarySearch(key);
      if (idx >= 0)
        return app.lstVals[idx];
      return "";
    }
  }
}


If you are using Apache and wanna test it just put the EXE in the cgi-bin folder under Apache's directory. Also note that the parameters are static just for this example.
Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 02:25 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 10:53 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:44 delete

    moneyideas