473,320 Members | 1,926 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,320 software developers and data experts.

ERROR: The remote server returned an error: (401) Unauthorized.

I get this error when i run my code below

"An unhandled exception of type 'System.Net.WebException' occurred in
system.dll
Additional information: The remote server returned an error: (401)
Unauthorized."

I am attempting to access a secure https site which
1-the site gives you your own certificate to use to access the site
2-a uname/password challenge i am not sure if it is basic or digest but i
have tried both

I can manually enter the site using a browser
1- the site asks for a cert which you will need select before going onto the
username/password area no domain
2- uname/pwd entered the correct password
3- the site then sends you to the correct area for which you are authorized

manualy everythign works fine so i know the uname/pwd are correct
at 1 point i was getting a 403 forbidden error but adding line 12 solved
that

its not very complex and probably a newbie mistake and i know i did not add
error handling yet, i want it to error out on any problems,

I am trying to access the site to upload some HR information. and no ADP is
not supporting this

Thank you in advance for any help
Leonard Danao

[code=vb]
1 ServicePointManager.CertificatePolicy = New
AcceptAllCertificatesPolicy
2
3 Dim ADPPayrollwebsite As String = "https://<Some ADP Site"

4 Dim userName As String = "<uname>"
5 Dim userPassword As String = "<pwd>"
6 Dim AuthDomain As String = "<domain>"
7 Dim OneCred As NetworkCredential
8 Dim CredCache As CredentialCache

9 ' The path to the certificate.
10 Dim Certificate As String = "<cer>"

11 ' Load the certificate into an X509Certificate object.
12 Dim cert As X509Certificate =
X509Certificate.CreateFromCertFile(Certificate)

13 Dim encoding As New ASCIIEncoding
14 Dim PostData As String = ""
15 Dim Data() As Byte = encoding.GetBytes(PostData)
16 Dim LoginReq As HttpWebRequest =
WebRequest.Create(ADPPayrollwebsite)

17 OneCred = New NetworkCredential(userName, userPassword) ',
AuthDomain)

18 Dim wrCache As CredentialCache = New CredentialCache
19 wrCache.Add(New Uri(ADPPayrollwebsite), "digest", New
NetworkCredential(userName, userPassword)) 'OneCred)

20 With LoginReq
21 .ClientCertificates.Add(cert)
22 .PreAuthenticate = False
23 .Credentials = wrCache
24 .KeepAlive = False
25 .Method = "POST"
26 .AllowAutoRedirect = True
27 .ContentType = "application/x-www-form-urlencoded"
28 .ContentLength = Data.Length
29 End With

30 Dim sendreq As Stream = LoginReq.GetRequestStream

31 sendreq.Write(Data, 0, Data.Length)
32 sendreq.Close()

33 Dim loginres As HttpWebResponse = LoginReq.GetResponse()
34 Dim sreader As StreamReader = New
StreamReader(loginres.GetResponseStream)
35 Dim HTML As String = sreader.ReadToEnd

36 txbTestArea.Text = ADPPayrollwebsite & vbCrLf ' Site/Page
currently visiting

37 ' View headers for this connection
38 Dim i As Integer
39 While i < loginres.Headers.Count
40 txbTestArea.Text = txbTestArea.Text & loginres.Headers.Keys(i)
& " : " & loginres.Headers(i) & vbCrLf
41 i = i + 1
42 End While
43 'view the URI that responded to this request
44 txbTestArea.Text = txbTestArea.Text &
loginres.ResponseUri.ToString & vbCrLf
45 loginres.Close()

[\code]
Nov 22 '05 #1
1 6751
also i would like to point out that line 14 has data, but i did not place it
there due to condifential information

Thanks

"Leonard Danao" <ld****@optonline.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I get this error when i run my code below

"An unhandled exception of type 'System.Net.WebException' occurred in
system.dll
Additional information: The remote server returned an error: (401)
Unauthorized."

I am attempting to access a secure https site which
1-the site gives you your own certificate to use to access the site
2-a uname/password challenge i am not sure if it is basic or digest but i
have tried both

I can manually enter the site using a browser
1- the site asks for a cert which you will need select before going onto the username/password area no domain
2- uname/pwd entered the correct password
3- the site then sends you to the correct area for which you are authorized
manualy everythign works fine so i know the uname/pwd are correct
at 1 point i was getting a 403 forbidden error but adding line 12 solved
that

its not very complex and probably a newbie mistake and i know i did not add error handling yet, i want it to error out on any problems,

I am trying to access the site to upload some HR information. and no ADP is not supporting this

Thank you in advance for any help
Leonard Danao

[code=vb]
1 ServicePointManager.CertificatePolicy = New
AcceptAllCertificatesPolicy
2
3 Dim ADPPayrollwebsite As String = "https://<Some ADP Site"

4 Dim userName As String = "<uname>"
5 Dim userPassword As String = "<pwd>"
6 Dim AuthDomain As String = "<domain>"
7 Dim OneCred As NetworkCredential
8 Dim CredCache As CredentialCache

9 ' The path to the certificate.
10 Dim Certificate As String = "<cer>"

11 ' Load the certificate into an X509Certificate object.
12 Dim cert As X509Certificate =
X509Certificate.CreateFromCertFile(Certificate)

13 Dim encoding As New ASCIIEncoding
14 Dim PostData As String = ""
15 Dim Data() As Byte = encoding.GetBytes(PostData)
16 Dim LoginReq As HttpWebRequest =
WebRequest.Create(ADPPayrollwebsite)

17 OneCred = New NetworkCredential(userName, userPassword) ',
AuthDomain)

18 Dim wrCache As CredentialCache = New CredentialCache
19 wrCache.Add(New Uri(ADPPayrollwebsite), "digest", New
NetworkCredential(userName, userPassword)) 'OneCred)

20 With LoginReq
21 .ClientCertificates.Add(cert)
22 .PreAuthenticate = False
23 .Credentials = wrCache
24 .KeepAlive = False
25 .Method = "POST"
26 .AllowAutoRedirect = True
27 .ContentType = "application/x-www-form-urlencoded"
28 .ContentLength = Data.Length
29 End With

30 Dim sendreq As Stream = LoginReq.GetRequestStream

31 sendreq.Write(Data, 0, Data.Length)
32 sendreq.Close()

33 Dim loginres As HttpWebResponse = LoginReq.GetResponse()
34 Dim sreader As StreamReader = New
StreamReader(loginres.GetResponseStream)
35 Dim HTML As String = sreader.ReadToEnd

36 txbTestArea.Text = ADPPayrollwebsite & vbCrLf ' Site/Page
currently visiting

37 ' View headers for this connection
38 Dim i As Integer
39 While i < loginres.Headers.Count
40 txbTestArea.Text = txbTestArea.Text & loginres.Headers.Keys(i) & " : " & loginres.Headers(i) & vbCrLf
41 i = i + 1
42 End While
43 'view the URI that responded to this request
44 txbTestArea.Text = txbTestArea.Text &
loginres.ResponseUri.ToString & vbCrLf
45 loginres.Close()

[\code]

Nov 22 '05 #2

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

Similar topics

0
by: sirumalla | last post by:
These posting i have seen in so many sites but none of them solved the problem. Iam trying to fix this problem past 2 days but iam not able to do so. I doubt whether iam missing any peice of code ...
2
by: Vijayakrishna Pondala | last post by:
Hi, I am using the following C# code to connect to and download information from a URL. (The pages are on JBoss server and we are using NTLM authentication to connect to the server). ----...
3
by: Vijayakrishna Pondala | last post by:
Hi, I am using the following C# code to connect to and download information from a URL. (The pages are on JBoss server and we are using NTLM authentication to connect to the server). ----...
2
by: obeOnline | last post by:
I'm getting the following error... System.Net.HttpWebRequest.CheckFinalStatus() +676 System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) +139...
0
by: sirumalla | last post by:
These posting i have seen in so many sites but none of them solved the problem. Iam trying to fix this problem past 2 days but iam not able to do so. I doubt whether iam missing any peice of code ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.