Wednesday, May 23, 2007

Using FtpWebRequest & FtpWebResponse to Upload File

Uri FtpHost = new Uri("ftp://127.0.0.1/wrar351.exe"); //Have to include destination filename in Uri()

FtpWebRequest ftprq;

FtpWebResponse ftprp;

string FtpFileName=@"c:\wrar351.exe"; //upload a WinRar self-install file

 

ftprq = (FtpWebRequest)WebRequest.Create(FtpHost);

ftprq.EnableSsl = false;

ftprq.UseBinary = true;

ftprq.UsePassive = false;

ftprq.Method = WebRequestMethods.Ftp.UploadFile;

ftprq.Credentials = new NetworkCredential("username", "password");

 

Stream RequestStream = ftprq.GetRequestStream();

Console.WriteLine("Put file:" + FtpFileName);

using (FileStream fs = new FileStream(FtpFileName, FileMode.Open))

{

    byte[] buf = new byte[4096];

    int readcount = 0;

 

    while ((readcount = fs.Read(buf, 0, 4096)) != 0)

    {

        RequestStream.Write(buf, 0, readcount);

        Console.Write("*");

    }

}

RequestStream.Close();

ftprp = (FtpWebResponse)ftprq.GetResponse();

 

if (ftprp.StatusCode != FtpStatusCode.ClosingData) //upload complete status is Closing Data

{

    throw new IOException("Ftp upload Error: " + ftprp.StatusCode.ToString() + ftprp.StatusDescription);

}

Console.Write("\nFile Upload OK\n");

ftprp.Close();

return;

Friday, May 11, 2007

Write file >2G on AIX

To write file over 2G on AIX, you should check the following settings:

  1. Check /etc/security/limits and /etc/security/olimits. Make sure fsize is -1
  2. Issue an "lsfs -q" command, check if "bf" is enabled

Name            Nodename   Mount Pt               VFS   Size    Options    Auto Accounting

/dev/hd4        --         /                      jfs   786432  --         yes  no  
(lv size: 786432, fs size: 786432, frag size: 4096, nbpi: 2048, compress: no, bf: false, ag size: 8)

Thursday, May 10, 2007

Compute string's MD5 hash value

string s; //User Input string

string md5 = "61a60170273e74a5be90355ffe8e86ad"; //MD5ed hash string

System.Text.StringBuilder sb = new System.Text.StringBuilder();

System.Security.Cryptography.MD5CryptoServiceProvider hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();

byte[] hashed;

 

hashed = hasher.ComputeHash(System.Text.Encoding.Default.GetBytes(s));

 

for (int i = 0; i < hashed.Length; i++)

{

    sb.Append(hashed[i].ToString("x2"));

}

 

if (s == md5)

{

    //...

}

Thursday, April 19, 2007

Configuring log4net at Runtime

If you want to set up log4net in runtime instead of using XML configuration file, you can use the following code. After setting up options, do not forget to call ActivateOptions() method.

private void SetupLog()

{

    log4net.Appender.RollingFileAppender rLog = new log4net.Appender.RollingFileAppender();

    log4net.Layout.PatternLayout pl = new log4net.Layout.PatternLayout();

 

    pl.Header = "=================\r\nStart of Program log\r\n";

    pl.Footer = "=================\r\nEnd of Program log\r\n";

 

    pl.ConversionPattern = "%d [%t] %-5p %c - %m%n";

    pl.ActivateOptions();

    rLog.File = "Program.log";

 

    rLog.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;

    rLog.LockingModel = new log4net.Appender.FileAppender.MinimalLock();

    rLog.AppendToFile = true;

    rLog.Layout = pl;

    rLog.DatePattern = "yyyyMMdd";

    rLog.StaticLogFileName = true;

 

    rLog.ActivateOptions();

    log4net.Config.BasicConfigurator.Configure(rLog);

}

Wednesday, April 18, 2007

Loading UserControl at Runtime and Calling Function in UserControl

If you want to load ASP.NET User Control at runtime and calling methods in the UserControl class (such as filling data in the control), you can use the following code. Use reflection to get method in the UserControl then invoke it.

UserControl uc = null;


uc = (UserControl)LoadControl("UserControl.ascx");

Type ucType = uc.GetType();

MethodInfo mi = ucType.GetMethod("FillData");

string returnValue=(string)mi.Invoke(uc, new object[] { arg1, arg2, arg3});

Using HttpWebRequest & HttpWebResponse to POST Data

If you want to get a web page, use HttpWebRequest to make request, and use HttpWebResponse to get response. The following code demostrates how to make request by POST method, then get response from web server. You can get response content by manipulating stream st.

HttpWebRequest hwr;


hwr = (HttpWebRequest)WebRequest.Create(url);


hwr.Method = "POST";


string postdata = "formpostdata=" + System.Web.HttpUtility.UrlEncode(dataToPost);

 

ASCIIEncoding enc = new ASCIIEncoding();

 

byte[] bytel = enc.GetBytes(postdata);

 

hwr.ContentType = "application/x-www-form-urlencoded";

hwr.ContentLength = bytel.Length;

 

//write to request stream

Stream newstream = hwr.GetRequestStream();

newstream.Write(bytel, 0, bytel.Length);

newstream.Close();

 

hwr.Timeout = 10000;

 

//get response

HttpWebResponse hwresp = (HttpWebResponse)hwr.GetResponse();


Stream st = hwresp.GetResponseStream();

Using .NET to Build a XML DOM

XmlDocument xmldoc = new XmlDocument();

XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");


xmldoc.InsertBefore(xmldec, xmldoc.DocumentElement);


XmlElement xmluser = xmldoc.CreateElement("userdata");

xmldoc.DocumentElement.PrependChild(xmluser);


XmlElement name = xmldoc.CreateElement("name");

name.AppendChild(xmldoc.CreateTextNode("abc123"));

xmluser.AppendChild(name);

 

XmlElement id = xmldoc.CreateElement("id");

id.AppendChild(xmldoc.CreateTextNode("A100000000"));

xmluser.AppendChild(id);