473,387 Members | 3,033 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,387 software developers and data experts.

How can you easily check the http error number of a webclient?

19
I'm trying to detect the http status number such as (401 Unauthorized) from a 'WebException' when a WebClient in my code fails.

Expand|Select|Wrap|Line Numbers
  1. Public Function DataSources_ValidURL() As Boolean
  2.         Dim TestClient As New WebClient()
  3.         TestClient.BaseAddress = DataSources_FilePath
  4.         Try
  5.             TestClient.DownloadString(DataSources_FilePath)
  6.         Catch ex As WebException
  7.             If ex.Status = 401 Then
  8.                 MessageBox.Show("Testing")
  9.             End If
  10.             If ex.Response.Headers.AllKeys.Contains("WWW-Authenticate") Then
  11.                 If MessageBox.Show("The remote server is requesting a username and password.", "Password Required", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
  12.  
  13.                 Else
  14.  
  15.                 End If
  16.             End If
  17.  
  18.             MessageBox.Show(String.Format("An error was encountered whilst trying to connect to the entered web address: {0}", ex.Message), "Error Encountered")
  19.             Return False
  20.         End Try
  21.  
  22.         Return True
  23.     End Function
I would rather check for the error number then for an authentication header...

Expand|Select|Wrap|Line Numbers
  1.  If ex.Response.Headers.AllKeys.Contains("WWW-Authenticate") Then
Does anyone know how to simply check for a 401 status return from a WebException.

Thanks
Dec 20 '09 #1
0 1345

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Justin Malloy | last post by:
I am using the System.Net.Webclient to try and download an XML file from a website but am receiving a HTTP protocol error when running the DownloadFile() sub routine. I did a HTTP trace using...
4
by: Henry Jia | last post by:
Hi, Does there has any easy way to check if a specified url is a valid one (both the format should be valid but also the specified path / file is available)? Thanks in advance! Henry
6
by: genc ymeri | last post by:
Hi, We are struggeling to upload a file through a C# webClient into JBoss web server. Meanwhile we are able to upload a file from the webserver itself. The problem is only with C# webClient . The...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
0
by: Helen | last post by:
Hi, I am trying to use a .Net WebClient object to read data from a url, but I am getting the following error: System.Net.HttpWebRequest.CheckFinalStatus()...
3
by: jmhmaine | last post by:
How do I programmatically read an SSL certificate when using connecting with WebClient? I need to verify the domain name matches the certificate and that the Expiration Date is valid before I post...
11
by: belgie | last post by:
How can I retrieve an image (.jpg) from a web site using http with VB dotnet? thanks Bill
0
by: Saverio Tedeschi | last post by:
Hi all gurus, I wrote an Win app with embedded FTP client (well, made some cut and paste from others' projects :-)) to receive and send files from within the app itself. Now the server I connect...
1
by: Mad Scientist Jr | last post by:
For some reason I can't get a WebClient to access an outside URL from behind our firewall. The code works when it runs outside the firewall. I turned on windows authentication in the web.config...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.