0

如何使用 Mono For Android 创建 Web 服务?似乎一切都是关于使用 Web 服务,而不是真正创建一个。

我试过用这个:http ://www.mono-project.com/Writing_a_WebService

但是 System.Web.Services.WebService 不存在。System.ServiceModel 也尚未翻译。有没有人知道如何在 Mono For Android 上创建 web 服务?

谢谢

4

1 回答 1

0

我现在尝试实现以下代码并尝试在模拟器中运行它,但是我通过浏览器或通过 REST 客户端发出的请求从未到达 HandleRequest。

protected override void OnCreate(Bundle bundle) {
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    var startBtn = FindViewById<Button>(Resource.Id.StartBtn);
    stopBtn.Clickable = false;

    startBtn.Click += SetupListener;
}

private void SetupListener(object sender, EventArgs e) {
    _httpListener = new HttpListener();
    _httpListener.Prefixes.Add("http://*:9876/");
    _httpListener.Start();
    _httpListener.BeginGetContext(HandleRequest, _httpListener);
}

private void HandleRequest(IAsyncResult result) {
    var context = _httpListener.EndGetContext(result);
    var response = "<html>Hello World</html>";
    var buffer = Encoding.UTF8.GetBytes(response);

    context.Response.ContentLength64 = buffer.Length;
    context.Response.OutputStream.Write(buffer, 0, buffer.Length);
    context.Response.OutputStream.Close();

    _httpListener.BeginGetContext(HandleRequest, _httpListener);
}

I have tried making request like the following: http:// localhost:9876/ , http:// 10.1.1.190:9876/ and http:// 10.0.2.2:9876/ but none of them actually reaches into the application.

于 2012-08-14T15:04:21.680 回答