: .NET/C#




Get URL Response

July 18th, 2008 admin Posted in .NET/C# Comments Off


public string GetSiteContents(string url)
{
	StringBuilder sb  = new StringBuilder();
	byte[] buf = new byte[8192];
	HttpWebRequest  request  = (HttpWebRequest)	WebRequest.Create(url);
	HttpWebResponse response = (HttpWebResponse) request.GetResponse();

	Stream resStream = response.GetResponseStream();
	string tempString = null;
	int count = 0;
	do
	{
		count = resStream.Read(buf, 0, buf.Length);
		if (count != 0)
		{
			tempString = Encoding.ASCII.GetString(buf, 0, count);
			sb.Append(tempString);
		}
	}
	while (count > 0);

	return sb.ToString();
}
	
AddThis Social Bookmark Button

Cookies

July 18th, 2008 admin Posted in .NET/C# Comments Off


private void Page_Load(object sender, System.EventArgs e)
{
	if (!IsPostBack)
	{
		if (Request.Cookies["MuneebCookies"]!=null)
		{
			HttpCookie httpCookie = Request.Cookies["MuneebCookies"];
			TextBox1.Text = httpCookie["FirstName"];
			TextBox2.Text = httpCookie["LastName"];

		}
	}
}
private void Button1_Click(object sender, System.EventArgs e)
{
	HttpCookie httpCookie = new HttpCookie("MuneebCookies");
	httpCookie.Values.Add("FirstName", TextBox1.Text);
	httpCookie.Values.Add("LastName", TextBox2.Text);
	httpCookie.Expires  = DateTime.Now.AddHours(2);
	// httpCookie.Expires  = DateTime.Now.AddYears(50);
	// NEVER EXPIRES (practically)
	Response.Cookies.Add(httpCookie);
}

AddThis Social Bookmark Button

SMTP Mail

July 18th, 2008 admin Posted in .NET/C# Comments Off


using System;
using System.Web.Mail;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
	MailMessage Message = new MailMessage();
	Message.To = "to_address@domain.com";
	Message.From = "from_address@domain.com";
	Message.Subject = "test1";
	Message.Body = "test1";

	try
	{
		Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
		Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "muneebsa");
		Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "you03ahm");
		Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
		Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.sbcglobal.yahoo.com");
		Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", 10);
		Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
		Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", false); 

		SmtpMail.SmtpServer = "smtp.mydomain.com";
		SmtpMail.Send(Message);
	}
	catch(System.Web.HttpException ehttp)
	{
		Console.WriteLine("{0}", ehttp.Message);
		Console.WriteLine("Here is the full error message output");
		Console.Write("{0}", ehttp.ToString());
	}

}
}
}

AddThis Social Bookmark Button

Simple Database Access

July 18th, 2008 admin Posted in .NET/C# Comments Off


string connectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;USER=root;PASSWORD=mypassword;OPTION=3;";
IDbConnection conn = new OdbcConnection(connectionString);
conn.Open();
IDbCommand command = new OdbcCommand("select UserID, FirstName from Users");
command.Connection = conn;
IDataReader reader = command.ExecuteReader();
while (reader.Read())
{
	string userID = (string) reader["UserID"];
	string firstName = (string) reader["FirstName"];
	Console.WriteLine("@@");
}
conn.Close();
AddThis Social Bookmark Button

Similar Posts