Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘URL’

Get Silverlight XAP and Hosting Page URL

Posted by Viral Sarvaiya on December 5, 2011

Get the URL to the current xap file:


Get the full URL of the page hosting the xap (with QueryStrings):



Posted in .Net, ASP.NET, Silverlight, Visual Studio | Tagged: , , , , , | Leave a Comment »

Get HTML code from URL in ASP.NET

Posted by Viral Sarvaiya on November 10, 2009

To get HTML of web page you need only few lines of code.

To start, place two TextBox controls named txtURL and txtPageHTML, and one button control on web form, like in image bellow:

Web form for getting page HTML at design time

Now, on button’s click event function, place this code:

[ C# ]

// We  need these namespaces
using  System;
using  System.Text;
using  System.Net;

public partial class DefaultCS : System.Web.UI.Page

protected void  btnGetHTML_Click(object sender, EventArgs e)
// We'll use WebClient class for reading HTML of web  page
WebClient MyWebClient = new WebClient();

// Read web page HTML to byte array
Byte[] PageHTMLBytes;
if (txtURL.Text !=  "")
PageHTMLBytes =  MyWebClient.DownloadData(txtURL.Text);

// Convert result from byte array to string
// and display it in TextBox txtPageHTML
UTF8Encoding oUTF8 =  new UTF8Encoding();
txtPageHTML.Text  = oUTF8.GetString(PageHTMLBytes);

[ VB.NET ]

‘ We need these namespaces

Imports  System
Imports  System.Text
Imports  System.Net

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnGetHTML_Click(ByVal  sender As Object, ByVal e As  System.EventArgs) Handles btnGetHTML.Click
' We'll use WebClient class for reading HTML of web  page
Dim MyWebClient As  WebClient = New WebClient()

' Read web page HTML to byte array
Dim PageHTMLBytes() As Byte
If txtURL.Text <> "" Then
PageHTMLBytes =  MyWebClient.DownloadData(txtURL.Text)

' Convert result from byte array to string
' and display it in TextBox txtPageHTML
Dim oUTF8 As  UTF8Encoding = New UTF8Encoding()
txtPageHTML.Text =  oUTF8.GetString(PageHTMLBytes)
End If
End Sub

End Class

Now you can start sample project, type some valid URL in first TextBox control and click to “btnGetHTML” button. Code listed above will return HTML code of requested URL and display it in second text box, like in image bellow:

HTML code is read and shown in text box

As you see, loading of HTML code of web page is relatively easy. Analyzing of this data is much harder and depends of page structure.

Reference :

Posted in ASP.NET | Tagged: , , , , | 5 Comments »

Www automaticaly in url if not exists

Posted by Viral Sarvaiya on July 1, 2009

<script language=”javascript”>
var sLink = window.location.href.toLowerCase(); ;
if (sLink.indexOf(“http://&#8221;) > -1) {
if (sLink.indexOf(“www.”) == -1) {
sLink = sLink.replace(“http://&#8221;, “http://www.&#8221;);
window.location.href = sLink;

Put this script in head tag of your page

Posted in Javascript | Tagged: , , | Leave a Comment »

%d bloggers like this: