Thursday, May 15, 2008

Learning About .NET Web Access Classes

I mentioned about wanting to start a business in my previous post. Well it looks like I might have my chance. While it isn't exactly what I was thinking of in my previous post, I foresee many opportunities to flex my programming muscle in this endeavor. Plus, I will be starting up with a good friend, so there is a good chance our motivation will actually produce some results.

A piece of software I am starting to research/design/create will be an internal application we will use to automatically extract some government provided public data. The current issue is time. With the amount of data and the requirement of using their web interface, it is not worth the time needed to do it by hand. The only other option would be to buy the data from the government, but that's just an unnecessary cost seeing as there is a free option available.

So I will be writing a program in C# to automatically access a few websites and download the needed data in chunks. I have never delt with .NET's web access objects, so I started looking at what they have to offer today.... which looks like a lot.

I modified an example from here:
http://msdn.microsoft.com/en-us/library/456dfw4f.aspx

To do a simple test to see how reading a web page works in .NET.

My revised code below:
Take note of the two windows form controls (webBrowser1) and (textbox1).


//due to the finally statement, these variables need to be created outside the try block
WebRequest request = null;
WebResponse response = null;
Stream dataStream = null;
StreamReader reader = null;
try
{
// Create a request for the URL.
request = WebRequest.Create("http://page_to_access");

// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;

// Get the response.
response = request.GetResponse();

// Display the status.
textBox1.Text = ((HttpWebResponse)response).StatusDescription;

// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();

// Open the stream using a StreamReader for easy access.
reader = new StreamReader(dataStream);

// Read the content.
string responseFromServer = reader.ReadToEnd();

// Display the content.
webBrowser1.DocumentText = responseFromServer;
}
catch (Exception error)
{
Console.Write(error.ToString());
}
finally
{
// Clean up the streams and the response.
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
}


All it does is just request a webpage (only with get data) and then stream the response to a string. After that it takes the string and inserts it into the standard WebBrowser control.

Next up I will setup/research:
- Searching the results from a web request.
- POSTing data to a page as apposed to using the get string
- Downloading files that are available from a webpage

Should be interesting!

Sunday, May 4, 2008

JavaScript Tip: Submit Form On Enter Key

A common practice with search forms is to have them submit when the enter key is pressed, instead of requiring the user to use their mouse to press the submit button (or using tab key to tab to the submit button).

Here is a simple way to do that in JavaScript:
First, define a input box:


<input name="txtsearchentries" id="txtsearchentries" type="text" size="13" value="" />


Second, create a function to be placed in the head area of the webpage:
//see if the user pressed the enter key while using a specific control

function submitSearchOnEnter(variable)
{
var keyCode;

//get whatever key was just pressed
//try to account for different methods of getting
//event keys depending on implementation
if(window.event)
{
keyCode = window.event.keyCode;
}
else if(variable)
{
keyCode = variable.which;
}

//if the key was the enter key, perform the necessary function
if(keyCode == 13) //13 = enter key
{
//here is where you process the form item or call a function to do that
searchEntries(); //call search
}
}


Third, you need to assign the function above to to be an event handler:

//assign an event handler to the search box so we can have it
//submit when user presses enter
document.getElementById('txtsearchentries').onkeyup = submitSearchOnEnter;

That's it. Should be able to assign it similarly to other fields on the page too, but then you would need to think of how to create separate functions to handle each.

Thursday, May 1, 2008

Work and Work

Work has been frustrating me lately. I work with some people who have big ideas and not that much technical knowledge. Well that isn't the problem more then I have been working on things for multiple departments (outside of my core area) and they seem to all keep coming up with ideas for their respective projects. My boss is a nice guy and likes to please everyone, but that is pushing me towards feature creep hell. Ok, I'm exaggerating, but if you have ever had a job in software development you know the feeling.

On a side note, I've been thinking more about the idea of my own software/web related business. I started looking into Code Igniter (codeigniter.com), which is an open source php framework that should help me save time in personal projects. Sure, I have a multitude of ideas, but getting that effort after work or on the weekends to actually accomplish something is difficult.

頑張るよ as they say in Japan (will try my best!).