January 2008 - Posts

In this blog post, i will explain how we used the HttpWebRequest to retrieve a page which needs authentication.
My colleague had an issue in which he needed to retrieve a specific quotes from http://finance.yahoo.com. Of course to be able to retrieve his own created table, he needs to authenticate using his yahoo credentials. My idea was to use HttpWebRequest class by sending the proper authentication cookie in its header which will guarantee the page retrieval.
To retrieve the authenticated header cookie value, i used Fidler to intercept the request and response. After authentication was made, i looked at the filder and get the cookie value from the authenticated request header. In this way, if i added to the HttpWebRequest header collection the "Cookie" and its retrieved value, The web server will deal with my request as an authenticated one.

Below is the implementation (Import System.Net and System.IO)

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://finance.yahoo.com/p?k=pf_1&d=v4");
            request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            request.Headers.Add("Cookie", "TheEncryptedValueRetrieved");
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    string strResult = sr.ReadToEnd();
                    StreamWriter sw = new StreamWriter(Server.MapPath("~/test.html"));
                    sw.WriteLine(strResult);
                    // Close StreamWriter and StreamReader
                    sr.Close();
                    sw.Close();
        }
         }
                    Response.Redirect("test.html");
         catch(Exception ex)
         {
        Response.Write(ex.Message);
         }

N.B: In my network, Internet access is under a proxy server which was displaying "The remote server returned an error: (407) Proxy Authentication Required.". To fix this issue, i had to add request.Proxy.Credentials = CredentialCache.DefaultCredentials; to the request object which will set the proxy credentials to the default one already configured.

The Encrypted value of the Cookie is replaced in this example by "TheEncryptedValueRetrieved" for security purpose.

Hope this helps

Posted by haissam | 3 comment(s)


Server side validation will be ignored in case you are using the Cross Page Posting feature in ASP.NET 2.0 and therefore you can't know what was the user input. However on the redirected page, you can still check the previous page IsValid method. In such  scenario, if the IsValid property of the previous page is false, you can redirect the user back to the previous page but all the input fields will be losing their data because like you might know the ViewState is not maintained.

Below is code implementation:

protected void Page_Load(object sender, EventArgs e)
{
 if(!PreviousPage.IsValid)
    {
  Server.Transfer("Default.aspx");
    }
 else
    {
  // Execute the code
    }
}

HC

Posted by haissam | 1 comment(s)

How many of you used group box control when developing Desktop Applications! I used them to group controls on a specific form in a logical more organized way with titles. Now in ASP.NET 2.0 and using Panel web server control, this can be achieved. Below is a sample HTML to demonstrate it

        <asp:Panel ID="Panel1" runat="server" Width="100%" Height="30%">
        <fieldset>
        <legend id="pnlLegend" runat="server">
        Welcome
        </legend>
        </fieldset>
        </asp:Panel>

Adding the fieldset control inside the panel will allow us to add legend to it.

Once you copy this HTML into the Source, go back to the Design mode and check it up.

Don't forget you can treat legend now as a normal html control which can be transformed into HTML server control by adding runat="server" and id="pnlLegend" to set its text from code behind : pnlLegend.InnerText = "Hello World";

  

Posted by haissam | with no comments

The SharpOS project is aimed at writing an operating system in 100% C#.

Check out more about it in the below link

http://www.osnews.com/story.php/19100/SharpOS-Releases-First-Milestone

 

 

 

Posted by haissam | with no comments

Just to refresh our minds about strong references before starting to talk about weak references.A reference pointing to an object created by the new operator is called a Strong Reference and as long as this reference is still pointing to that object it will reside in memory. However, if a weak reference points to an object it gets collected by the garbage collector as soon as more memory is in need.

When to use Weak references?

For example, if you have data (not critical for your application) to be retrieved from an xml file, text file, or even a data source then you could use a weak reference. Even if it get collected by the garbage  collector you can still re-process the data.

 Below is a sample code to get a weak reference out of a strong one.

         // Create new strong reference to an object
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        // Load the xml into that object
        doc.Load(Server.MapPath("~/test.xml"));
        // Create weak reference out of doc object
        WeakReference docWeakRef = new WeakReference(doc);
        // Set the doc object to null;
        doc = null;

Now you can still get the strong reference to that object before using

docWeakRef.Target // which returns a strong reference to the weak object

Just remember before using the object always check the Target property if it is null then the object has been collected by the garbage collector and of course removed from the memory.

Happy Coding! 

 

Posted by haissam | with no comments

I just want to share with you the below excellent article ( best practices ) to go deeply on how to handle exceptions in your application whether it was a windows application, web application or even a webservice.

Exception Management in .NET

Happy Coding!

Posted by haissam | 2 comment(s)
The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.