请求失败,HTTP状态为401:调用Web服务时未授权 [英] The request failed with HTTP status 401: Unauthorized while calling web service

查看:150
本文介绍了请求失败,HTTP状态为401:调用Web服务时未授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Web服务的URL,我正在尝试在我的WebAPI项目中调用该Web服务.

I have URL of web service and I am trying to call that web service in my WebAPI project.

URL: http://domain/soap/wsdl11?services = XYZ_Service& sap-client = 007& sap-user = demo& sap-password = demo @ 123

我尝试过的代码:

Service.DemoService client = new Service.DemoService();
client.Credentials = new NetworkCredential("demo", "demo@123", "domain");
client.PreAuthenticate = true;
client.GetData("Test");

但显示如下:

请求失败,HTTP状态为401:未授权.

The request failed with HTTP status 401: Unauthorized.

上面的URL在浏览器中有效.

The above URL is working in the browser.

推荐答案

您可以在应用程序中使用服务引用.右键单击引用,单击以添加服务引用弹出窗口.您可以在其中添加名称的服务引用来添加Web服务URL(不带方法和参数).它将表现为普通的dll或任何其他引用.创建服务引用名称的实例,然后使用该实例从您的Web服务调用带有参数的任何方法(如果有参数的话).

You can use service reference in you application. Right click references click to Add Service Reference popup will appear where you can add your web service URL(without method and parameters) by give the service reference with name. It will behave as normal dll or any other reference. create instance of service reference name then use the instance to call which ever method from your web service with parameters if parameters exist.

这篇关于请求失败,HTTP状态为401:调用Web服务时未授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆