我的计算机上安装了WCF服务。我可以从Windows上的控制台应用程序成功连接到此服务,但如果我在Android上使用相同的代码则无效。
using System;
using System.ServiceModel;
using Android.App;
using Android.Widget;
using Android.OS;
namespace App1
{
[ServiceContract]
public interface ILogonService
{
[OperationContract]
string Logon(string appId);
}
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += ButtonOnClick;
}
private void ButtonOnClick(object sender, EventArgs eventArgs)
{
var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
var endPointUri = new Uri(@"http://192.168.1.241/edo/Euphrates.svc/ILogonService-Basic");
var factory = new ChannelFactory<ILogonService>(binding, new EndpointAddress(endPointUri));
factory.Credentials.UserName.UserName = "test";
factory.Credentials.UserName.Password = "test";
ILogonService service = factory.CreateChannel();
Console.WriteLine(service.Logon("test"));
}
}
}
我收到此错误:
“System.Net.WebException:处理网页时出错 要求:状态码401(未经授权):未经授权“.11-19 18:16:29.973 E / mono(1177):11-19 18:16:29.973 E / mono(1177): 未处理的例外:11-19 18:16:29.973 E / mono(1177): System.Net.WebException:处理Web请求时出错: 状态码401(未经授权):未经授权11-19 18:16:29.973 E / mono (1177):at System.ServiceModel.Channels.HttpRequestChannel + HttpChannelRequestAsyncResult.WaitEnd ()[0x00000] in:0 11-19 18:16:29.973 E / mono(1177):at System.ServiceModel.Channels.HttpRequestChannel.EndRequest (IAsyncResult结果)[0x00000] in:0 11-19 18:16:29.973 E / mono( 1177):在System.ServiceModel.Channels.HttpRequestChannel.Request (System.ServiceModel.Channels.Message消息,TimeSpan超时) [0x00000] in:0 11-19 18:16:29.973 E / mono(1177):at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Request (System.ServiceModel.Channels.Message消息,TimeSpan超时)[0x00000] 在:0 11-19 18:16:29.973 E / mono(1177):at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Request (System.ServiceModel.Description.OperationDescription od, System.Object []参数)[0x00000] in:0 11-19 18:16:29.973 E / mono (1177):at System.ServiceModel.MonoInternal.ClientRuntimeChannel.DoProcess (System.Reflection.MethodBase方法,System.String operationName, System.Object []参数)[0x00000] in:0 11-19 18:16:29.973 E / mono (1177):at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Process (System.Reflection.MethodBase方法,System.String operationName, System.Object []参数)[0x00000] in:0
我该如何解决这个问题?有什么想法吗?
由于