473,382 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

URL in C# Windows form project

Hi,
In my C# Windows form project, I want to verify that if a file
http://myip/myfile.doc exists, how am I supposed to do? (the myip is a
specific public IP)
Thanks for help.
Jason


Jul 7 '06 #1
3 2182
Jason Huang wrote:
Hi,
In my C# Windows form project, I want to verify that if a file
http://myip/myfile.doc exists, how am I supposed to do? (the myip is a
specific public IP)
Thanks for help.
Jason
Hi Jason,

You can use an HttpWebRequest object, and check to see if you get a '404'
back:

///
public bool CheckUrl ( string url )
{
HttpWebRequest wr = (HttpWebRequest) HttpWebRequest.Create( url );

wr.MaximumAutomaticRedirections = 4;
wr.MaximumResponseHeadersLength = 4;
wr.Credentials = CredentialCache.DefaultCredentials;

try
{
webRequest.GetResponse();
}
catch ( WebException )
{
return false;
}

return true;
}
///

--
Hope this helps,
Tom Spink
Jul 7 '06 #2
It should be noted that the MaximumAutomaticRedirections and
MaximumResponseHeadersLength properties are not required for this.

Also, it is not disposing of the WebResponse properly.

On top of that, you are not checking the HttpStatusCode property on the
HttpWebResponse to see if you get a valid return code.

Finally, I would use the HEAD HTTP method to make the request (assuming
it supports HTTP 1.1). This way, only the headers are returned, but not the
body. This way, the server does not waste resources sending the document
back (nor do you waste resources having them sent to you).

My code would be this:

public bool CheckUri(string uri)
{
// Get the request.
WebRequest request = WebRequest.Create(url);

// Set the method to "HEAD".
request.Method = "HEAD";

// Wrap in a try/catch.
try
{
// Get the response.
using (HttpWebResponse response = (HttpWebResponse)
request.GetResponse())
{
// Check the status code. This is the most common status code
// but you might want to check others based on your needs.
if (response.HttpStatusCode != HttpStatusCode.OK)
{
// Return false.
return false;
}
}
}
catch (WebException e)
{
// Return false.
return false;
}

return true;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tom Spink" <ts****@gmail.comwrote in message
news:em**************@TK2MSFTNGP05.phx.gbl...
Jason Huang wrote:
>Hi,
In my C# Windows form project, I want to verify that if a file
http://myip/myfile.doc exists, how am I supposed to do? (the myip is a
specific public IP)
Thanks for help.
Jason

Hi Jason,

You can use an HttpWebRequest object, and check to see if you get a '404'
back:

///
public bool CheckUrl ( string url )
{
HttpWebRequest wr = (HttpWebRequest) HttpWebRequest.Create( url );

wr.MaximumAutomaticRedirections = 4;
wr.MaximumResponseHeadersLength = 4;
wr.Credentials = CredentialCache.DefaultCredentials;

try
{
webRequest.GetResponse();
}
catch ( WebException )
{
return false;
}

return true;
}
///

--
Hope this helps,
Tom Spink

Jul 7 '06 #3
Nicholas Paldino [.NET/C# MVP] wrote:
It should be noted that the MaximumAutomaticRedirections and
MaximumResponseHeadersLength properties are not required for this.

Also, it is not disposing of the WebResponse properly.

On top of that, you are not checking the HttpStatusCode property on
the
HttpWebResponse to see if you get a valid return code.

Finally, I would use the HEAD HTTP method to make the request
(assuming
it supports HTTP 1.1). This way, only the headers are returned, but not
the
body. This way, the server does not waste resources sending the document
back (nor do you waste resources having them sent to you).
<snippedy-doo-dah>

Well, I didn't write a complete program. But I took most of that code from
MSDN, because I've never used an HttpWebRe<sponse/questobject before.

--
Hope this helps,
Tom Spink
Jul 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bob lambert | last post by:
Help I am trying to deploy to another pc a vb.net std 2002 windows form application. I am confused. I created a project - windows form I built form, compiled and debugged. I created a...
13
by: Lee Newson | last post by:
Hi, I have just written my first application using VB.NET. The app works fine when i am running it within .NET for debugging purposes, however when i try to run the app from the .exe file that...
4
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right and down to centralise it the form simply jumps...
0
by: tony | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file. One project that build a class library dll One project that build a windows control dll In...
3
by: garyusenet | last post by:
Dear Professionals, I have recently been using the wonderful krypton toolkit and am trying to use them in my small hobby application. I include this bit of info as an aside really because i'm...
1
by: RSH | last post by:
Hi, I have a silly question... I have a Windows Form project (VB.Net) that was created in Visual Studio 2003. The project runs great. Now I have to add the ability to run the same...
1
by: CCAGS | last post by:
I have a Windows forms project which is part of a larger project. When I built this project, it worked great and had no problems. I recently was running the main project and clicked on the menu...
4
by: Jason Huang | last post by:
Hi, I am thinking in comparing developing speed between C# Windows Form and C# Web Form applications. Assuming the scenario is that we have 2 C# coding engineers, engineer A has 2 years...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.