首页 文章

具有凭据的Xamarin Android SOAP Web服务返回401未授权

提问于
浏览
0

我在Visual Studio 2015 v4.2.2.11(Xamarin.forms Portable项目)中使用C#Xamarin . 我尝试解决需要身份验证的Web服务,但不断获得401 Unauthorized .

Web服务是作为Web服务发布的Navision代码单元 . 它接受请求XML并返回响应XML . 我已经将这种方式用于许多项目,并且从未遇到过身份验证问题 . 以前的项目是.Net Windows,UWP,Windows移动,Windows CE(紧凑框架)以及最近用WinDev编写的一些项目,我使用相同的通信 .

Web服务需要以网络密钥(用户,密码,域)的形式进行身份验证 . 在UWP项目中,我可以添加服务引用并以这种方式进行身份验证 . 这就像一个魅力!在Droid和iOS项目中,我无法添加服务引用,因此我不得不使用Web引用 . Web Reference对象确实具有属性Credentials,但似乎没有实现?

从Android模拟器,我可以浏览到默认服务器页面,但是当我尝试浏览到webservice时,浏览器也返回401 nauthorized,对登录或身份验证没有疑问 . 在Windows 10手机模拟器上,我可以浏览到Web服务页面,浏览器会询问我的凭据 .

因为UWP项目使用Service ref而不能使用Web ref,Android和iOS项目都使用Web Reference而不能使用服务引用,所以我不得不将webservice连接拉到平台项目中 .

我有一个类SoapService,我将webservice声明为DynamicsNAV,服务本身称为serviceHandler,要执行的操作称为ProcessRequests .

public class SoapService : ISoapService
    {
    DynamicsNAV.WebServiceHandler_PortClient serviceHandler;
        string soapURL = "http://192.168.7.101:7047/DynamicsNAV90/WS/CRONUS%20BELGI%C3%8B%20NV/Codeunit/WebServiceHandler";
    public SoapService()
        {
            serviceHandler = new DynamicsNAV.WebServiceHandler();
            serviceHandler.Url = soapURL;
            NetworkCredential nc = new NetworkCredential("wd", "wd", "VMNAV2016");
            serviceHandler.Credentials = nc;
            serviceHandler.PreAuthenticate = true;
        }
   ...

在SoapService中,我有公共函数GetDataAsync,我调用webservice并以多种方式尝试了这一点,都返回401 ...试试1:

serviceHandler.ProcessRequests(request, ref response);

抛出异常:StatusCode = System.Net.HttpStatusCode.Unauthorized

试试2:

serviceHandler.ProcessRequestsAsync(request, response);
serviceHandler.ProcessRequestsCompleted += ServiceHandler_ProcessRequestsCompleted;

抛出异常:StatusCode = System.Net.HttpStatusCode.Unauthorized

尝试3:

HttpWebRequest req = HttpWebRequest.CreateHttp(soapURL);
req.Method = "POST";
req.PreAuthenticate = true;
req.Credentials = new NetworkCredential("wd", "wd", "VMNAV2016");
byte[] postBytes = Encoding.ASCII.GetBytes(request);
req.ContentLength = postBytes.Length;
Stream postStream = req.GetRequestStream();
postStream.Write(postBytes, 0, request.Length);
postStream.Close();
WebResponse resp = req.GetResponse();
if (resp.ContentLength > 0)
{ }

抛出异常:StatusCode = System.Net.HttpStatusCode.Unauthorized

...我尝试调整Service.cs,但没有任何帮助?!?

有人可以帮我吗?我必须解决这个问题或解决方法......

在此先感谢,Wim

1 回答

  • 0

    问题解决了 . 我调整了Navision Web服务以接受基本身份验证...凭据确实已发送,因此通信可以正常工作 .

    感谢所有帮助过的人 .

    维姆

相关问题