Wednesday, June 29, 2011

(Webclient) The remote server returned an error: (403) Forbidden.

Using downloadstring of webclient returns an error as stated by the post title. The solution was to add user agent header to the webclient headers. The value of user agent can be found in the mozilla firebug.
Solution:
Code:
  string URL = string.Format(your_url);  
  newWebClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; rv:2.0)  
                          Gecko/20100101 Firefox/4.0");  
  string pageSource = newWebClient.DownloadString(URL);  
  pageSource = System.Web.HttpUtility.HtmlDecode(pageSource);  

Thursday, June 23, 2011

Show External IP address using C#

I experimented a snippet to get my ISP provided IP address using whatismyip. Using tutorials or code snippets from links, the web request just returned a 404 error. After reading the whatismyip API again,they have changed their automation URL. Below is the code snippet.

Code:
  //previous URL present on most tutorials which is not working  
  //string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp";   
  //updated URL from whatismyip API  
  string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";  
  WebClient wc = new WebClient();  
  UTF8Encoding utf8 = new UTF8Encoding();  
  string requestHtml = "";  
  requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));

Wednesday, June 15, 2011

Convert Epoch Time to DateTime in C#

Me and my fellow developer encountered a scenario to convert epoch time to datetime. The solution can be found in stack overflow.
Code:
private void Form1_Load(object sender, EventArgs e)  
{  
     //long tms = 1308139229;  
     long tms = 1308143650;  
     DateTime dt = FromUnixTime(tms);  
     MessageBox.Show(dt.ToShortDateString());  
} 
 
public DateTime FromUnixTime(long unixTime)  
{  
   var epoch = new DateTime(1970, 1, 1, 0, 0, 0, 
         DateTimeKind.Utc);  
   return epoch.AddSeconds(unixTime);  
}  

Thursday, June 9, 2011

Sorting List Generic collection (string)

This example uses List generic collection and will sort names in descending order. Names with New will be displayed on top of the list. This is applicable if you will be displaying products names with NEW in it's product name as recently displayed.
Code:
  List<string> Names = new List<string>();  
      private void FListSort_Load(object sender, EventArgs e)  
      {  
        Names.Add("Mike");  
        Names.Add("John");  
        Names.Add("Titch");  
        Names.Add("Harold");  
        Names.Add("Klent");  
        Names.Add("Thomas New");  
        Names.Add("Mary");  
        Names.Add("Fultron New");  
        Names.Add("Khayce");  
        Names.Add("Tim");  
        Names.Add("Joker New");  
        Names.Add("Linda");  
        Names.Add("Arthur New");  
        Names.Add("Baby Lee Jones");  
        Names.Add("Caspers New");  
      }  
      internal class MyComp  
      {  
          public int sort(string x, string y)  
          {  
            return x.IndexOf("New").CompareTo(y.IndexOf("New"));  
          }  
      }      
      private void btnSort_Click(object sender, EventArgs e)  
      {  
        MyComp comp = new MyComp();  
        txtSorted.Text = "Sort by Ascending!";  
        //Names.Sort(delegate(string x, string y)   
        //{ return x.IndexOf("New").CompareTo(y.IndexOf("New")); });  
        Names.Sort(comp.sort);  
        txtSorted.Text += Environment.NewLine;  
        txtSorted.Text += Environment.NewLine;  
        txtSorted.Text = "Sort by Descending!" ;  
        txtSorted.Text += Environment.NewLine;  
        txtSorted.Text += Environment.NewLine;  
        Names.Reverse();  
        foreach (string x in Names)  
        {  
          txtSorted.Text = txtSorted.Text + x + Environment.NewLine;  
        }        
      }

Reference: Developer Fusion Article

Wednesday, June 8, 2011

Posting Data To WebBrowser Control

Basically, it is possible to post data to web browser control. The code below, was referenced from MSDN, but i slightly modified some of it's functionalities..
Code:
  string url = "http://example.com";  
  string postData = String.Format("city=DC&Page={0:00}",pageNum);  
  byte[] Post = Encoding.UTF8.GetBytes(postData );  
  string AdditionalHeaders = "Content-Type: application/x-www-form-urlencoded";  
  //wbSample is the web browser control  
  //the WebNavigate method is just a simple method i created  
  //which simply assigns url to the browser, the post data and additional headers  
  WebNavigate(wbSample , ListURL, "", Post, AdditionalHeaders);