加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

调用webservice时报错The request failed with HTTP status 401:

发布时间:2020-12-17 01:27:42 所属栏目:安全 来源:网络整理
导读:Note: This is KB article is taken from MSDN?as it is for my own reference. SYMPTOMS 1. Create a new ASP.NET Web Service by using Visual C# .NET or Visual Basic .NET. 2. Name the project WebServiceTest . 3. By default, Service1.asmx is crea

Note: This is KB article is taken from MSDN?as it is for my own reference.

SYMPTOMS

1. Create a new ASP.NET Web Service by using Visual C# .NET or Visual Basic .NET.
2. Name the project WebServiceTest.
3. By default,Service1.asmx is created.
4. Uncomment the default WebMethod "HelloWorld()".
5. On Build menu,click Build Solution.
6. Turn off Anonymous access to WebServiceTest. To do this,follow these steps:
a. In Control Panel,double-click Administrative Tools.
b. Double-click Internet Information Services.
c. Expand Internet Information Services,and then locate the WebServiceTest virtual directory.
d. Right-click WebServiceTest,and then click Properties.
e. Click the Directory Security tab.
f. Under Anonymous access and authentication control,click Edit.
g. In the Authentication Methods dialog box,click to clear the Anonymous access check box.
h. Click to select the Integrated Windows authentication check box.

Note Verify that only Integrated Windows authentication is selected.
i. Click OK to close the Authentication Methods dialog box.
j. Click OK to close Properties.
7. On the Build menu,click Build Solution.
8. Type the following address in the browser to view the Service1 Web service description:
http://localhost/WebServiceTest/Service1.asmx
9. Test the HelloWorld WebMethod. The WebMethod works as expected.
10. Add a Web Reference to a test ASP.NET Web Application. To do this,follow these steps:
a. Create a new ASP.NET Web Application by using Visual C# .NET or Visual Basic .NET. Name the project WebServiceCaller.
b. By default,WebForm1.aspx is created.
c. In Solution Explorer,right-click References,and then click Add Web Reference.
d. In the Address text box,type the URL for WebServiceTest as follows:
http://localhost/WebServiceTest/Service1.asmx
e. Click Go or press ENTER,and then click Add Reference.
11. In Solution Explorer,right-click WebForm1.aspx,and then click View Code.
12. Append the following code to the Page_Load event:

Visual C# .NET Sample:
// Start an instance of the Web service client-side proxy. localhost.Service1 myProxy = new localhost.Service1(); Response.Write( myProxy.HelloWorld());
Visual Basic .NET Sample:
'Start an instance of the Web service client-side proxy. Dim myProxy As localhost.Service1 = New localhost.Service1() Response.Write(myProxy.HelloWorld())
13. On the Debug menu,click Start,and then view the application in the browser.
14. The error message that is discussed in the "Symptoms" section appears in the browser.
15. To resolve this problem,assign DefaultCredentials to the Credentials property of the Web service client-side proxy. To do this,insert the following code before the line "Response.Write( myProxy.HelloWorld())":

Visual C# .NET Sample:
myProxy.Credentials= System.Net.CredentialCache.DefaultCredentials;
Visual Basic .NET Sample:
myProxy.Credentials = System.Net.CredentialCache.DefaultCredentials
16.

Repeat step 13

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读