Posts

Showing posts from June, 2011

Donate

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: 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);

How To Show External IP Address Using C# And WebClient

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. //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));

How To Convert Epoch Time To DateTime In C#

Good morning awesome programmers, Me and my fellow teammate encountered a scenario to convert epoch time to datetime. The solution can be found in stack overflow. 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); }

Sorting List<T> Generic Collection String In C#

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. 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"

Posting Data To WebBrowser Control In C#

Good day! Basically, it is possible to post data to web browser control using C#. The code below, was referenced from MSDN, but i slightly modified some of it's functionalities. 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);

Donate