Hi,
Does anyone know how to acess a url from a windows application?.
I have an address http://serverport/Page.I want to acess this page from my windows application.
Regards, Harsh Suman
Hi,
Does anyone know how to acess a url from a windows application?.
I have an address http://serverport/Page.I want to acess this page from my windows application.
Regards, Harsh Suman
It's not clear what you want to do with the page.
If you want to display it on the form, you can use a WebBrowser
control.
If you want to get the response and process it, use the System.Net.WebClient
class.
I'm not sure what you're asking for,so I just give the answer to yet another way to interpret the question.
If you simply want to launch the default browser (to display a local or online html manual etc.), in windows (and probably similar in other OS'es) you can use some kind of "execute interface" to execute a correctly formatted url as the command, this will usually launch the default browser:
According to this page this code should launch a browser:
string targeturl= "http://stackoverflow.com";
try
{
System.Diagnostics.Process.Start(targeturl);
}
catch
(
System.ComponentModel.Win32Exception noBrowser)
{
if (noBrowser.ErrorCode==-2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
MessageBox.Show(other.Message);
}
(It looks pretty ugly with magic numbers for error codes, though...)
If you want to download an HTML or any file you can use the WebClient class.
Example:
/// <summary>
/// Downloads a file from the given location
/// </summary>
/// <param name="url">Location of the file</param>
/// <param name="dest">The destination of the downloaded file</param>
/// <returns>False if there was an error, else True</returns>
public bool DownLoad(string url, string dest)
{
WebClient client = new WebClient();
try
{
//Downloads the file from the given url to the given destination
client.DownloadFile(url, dest);
return true;
}
catch (WebException)
{
// Handle exception
return false;
}
catch (System.Security.SecurityException)
{
// Handle exception
return false;
}
catch (Exception)
{
// Handle exception
return false;
}
}