Web Service Error: “The request failed with HTTP status 401: Unauthorized.”

解决方法:

Samples.TimeWebService ws = new Samples.TimeWebService();

//下面这句提供授权
ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
string currentTime = ws.GetCurrentTime();

原理以及详细分析原文如下:两种解决方法第一种我尝试无效,并且从安全性上来说也是代码授权比较合理

原文Copy 

I’ve come across a number of developers who have run into this issue when trying to call a web service in Visual Studio 2005 Beta 2. The symptom of the problem is that when trying to call a web service that is running on the "ASP.NET Development Server" from another application, the web service throws a WebException with the message: “The request failed with HTTP status 401: Unauthorized.”

First and foremost, this issue is not related to debugging. If you run the application outside of Visual Studio, you’ll see the same result. Also noteworthy to mention is that when you run the web service by itself, everything works fine. It’s only when you call the web service from another application that it fails.

Why do I get this error?

The reason for this error is that by default, a web service project in Visual Studio 2005 Beta 2 required NTLM authentication. When you call the web service from another application, the application tries to call into the web service with anonymous access and fails. But, when you call the web service from the web service test page in Internet Explorer, your credentials are passed from IE to the web service which means that the call succeeds.

How do I fix this error?

There are two ways to fix this error.

1. Disable NTLM Authentication in Project Properties
If your web service is not intended to require authentication, the easiest way to fix this error is to turn off NTML authentication in a web service project. To do this, right click on the web service project and select “Property Pages”. On the “Start Options” page, uncheck “NTLM Authentication”. NOTE: disabling this option has security implications if you are running the web service in a Terminal Server environment. If the web service is on your local machine and it’s not being used in a Terminal Server environment, then you should be fine.

2. Pass User's Credentials from Calling Application to Web Service
If your web service is intended to require authentication and not anonymous access, then pass the credentials of the user from the application to the web service. Here is an example:

Samples.TimeWebService ws = new Samples.TimeWebService();
ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
string currentTime = ws.GetCurrentTime();

HabibH.
6:46 PM 5/25/2005.

原文地质http://blogs.msdn.com/habibh/archive/2005/05/25/421954.aspx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:在复习JavaScript过程中发现该错误,Failed to load resource: the server responded with a status of 404 (Not Found) favicon.ico。\[1\]这个错误通常是由于浏览器在加载网页时无法找到favicon.ico文件引起的。favicon.ico是网站的图标文件,用于显示在浏览器标签页上。如果网站没有提供favicon.ico文件,浏览器会尝试从服务器上获取该文件,但如果服务器上也没有该文件,就会出现404错误。这个错误通常不会影响网页的正常运行,只是浏览器在控制台中会显示这个错误信息。 问题: Failed to load resource: the server responded with a status of 401 (Unauthorized) 回答: 当浏览器在加载资源时,如果服务器返回的状态码是401 (Unauthorized),表示请求未经授权。这通常是因为用户没有提供有效的身份验证凭据或者凭据无效导致的。服务器需要用户提供有效的身份验证信息才能访问该资源。要解决这个问题,用户需要提供有效的身份验证凭据,例如用户名和密码,以便服务器能够验证用户的身份并授权访问该资源。 #### 引用[.reference_title] - *1* [报错解决——Failed to load resource: the server responded with a status of 404 (Not Found)](https://blog.csdn.net/m0_51511286/article/details/123127476)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Failed to load resource: the server responded with a status of 404 ()](https://blog.csdn.net/qq_46084827/article/details/127614719)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [js报错:Failed to load resource: the server responded with a status of 404 (Not Found):](https://blog.csdn.net/qq_42592823/article/details/115492391)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值