1 在Vs 中添加 web services 方式 的代码,是可以成功的,如下
DE_UnfreezeSOAPSoapBindingQSService client = new DE_UnfreezeSOAPSoapBindingQSService(); var key = Guid.NewGuid().ToString(); client.Credentials = new NetworkCredential("username", "pwd");
2 在VS中添加服务引用的方式
方式1
DE_UnfreezeClient client = new DE_UnfreezeClient(); client.ClientCredentials.UserName.UserName = "username"; client.ClientCredentials.UserName.Password = "pwd";
方式 2
DE_UnfreezeClient client = new DE_UnfreezeClient(); client.ClientCredentials.HttpDigest.ClientCredential = new NetworkCredential("username", "pwd"); ;
报错信息如下。 应该如何传递用户账号信息呢?
“System.ServiceModel.Security.MessageSecurityException”类型的未经处理的异常在 mscorlib.dll 中发生 其他信息: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="weblogic"'.
https://msdn.microsoft.com/en-us/library/ms789011.aspx
需要设置代码
BasicHttpBinding binding; Uri address = new Uri(serviceUrl); binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly); binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; binding.CloseTimeout = new TimeSpan(3, 0, 0); binding.OpenTimeout = new TimeSpan(3, 0, 0); binding.ReceiveTimeout = new TimeSpan(3, 0, 0); binding.SendTimeout = new TimeSpan(3, 0, 0); binding.MaxReceivedMessageSize = 65536000; EndpointAddress addr = new EndpointAddress(serviceUrl); _serviceFactory = new ChannelFactory<DE_Unfreeze>(binding, addr); _serviceFactory.Credentials.UserName.UserName = userName; _serviceFactory.Credentials.UserName.Password = pwd;