通过本地网络的URL调用子程序?

时间:2013-04-06 15:50:47

标签: .net windows vb.net winforms

我正在用Visual Basic .NET编写一个程序,在我的本地网络上的PC上执行操作。

我基本上寻找某种非常简单的解决方案,允许我:

  • 通过网络浏览器从网络上的其他计算机调用子例程/函数
  • 如果可能,将参数发布到网址中(请参阅下面的乐观示例)
  • 允许发送html的响应字符串(再次参见示例)

示例: 输入http://192.168.1.72/function/argument1/argument2将在该(本地联网的)机器上的winforms应用程序应用程序中运行'function'子/函数,并通过argument1和argument2(如果包含它们),然后将响应页/字符串返回给请求浏览器作为反馈

有人能指出我这样做的方法吗?我正在寻找一些相当简单但可靠的东西,因为它最终可能会全天候运行

2 个答案:

答案 0 :(得分:2)

  

我正在寻找一些相当简单但可靠的东西,因为它最终可能会全天候运行

我认为最简单的方法是使用WCF的WebServiceHost类:

[ServiceContract]
public class MyService
{
    [OperationContract, WebGet(UriTemplate = "/function/{argument1}/{argument2}")]
    public string AMethod(string argument1, string argument2)
    {
        return argument1 + " " + argument2;
    }
}

将此行添加到应用程序的formload(或任何其他入口点),

var wsh = new WebServiceHost(typeof(MyService), new Uri("http://0.0.0.0/MyService"));
wsh.Open();

并从浏览器中调用http://192.168.1.72/Myservice/function/a/b。就是这样。

----完整工作守则----

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            var wsh = new WebServiceHost(typeof(MyService), new Uri("http://0.0.0.0/MyService"));
            wsh.Open();
        }
    }

    [ServiceContract]
    public class MyService
    {
        [OperationContract, WebGet(UriTemplate = "/function/{argument1}/{argument2}")]
        public string AMethod(string argument1, string argument2)
        {
            return argument1 + " " + argument2;
        }

        ///******** EDIT ********
        ///
        [OperationContract, WebGet(UriTemplate = "/function2/{argument1}/{argument2}")]
        public Stream F2(string argument1, string argument2)
        {
            return ToHtml("<html><h1>" + argument1 + " " + argument2 + "</h1></HtmlDocument>");
        }

        static Stream ToHtml(string result)
        {
            var data = Encoding.UTF8.GetBytes(result);

            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html; charset=utf-8";
            WebOperationContext.Current.OutgoingResponse.ContentLength = data.Length;

            return new MemoryStream(data);
        }
        ///END OF EDIT
    }
}

修改

  

是否可以确定请求来自的IP地址?

var m = OperationContext.Current
        .IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

var address = m.Address;
  

任何使{arguments}可选的方法吗?

只需从UriTemplate属性中删除WebGet参数即可。然后你的网址将是

http://1192.168.1.72/MyService/AMethod?argument1=aaaa&argument2=bbb 

如果你想让ex的argument2可选,请调用

http://1192.168.1.72/MyService/AMethod?argument1=aaaa

和argument2将获得方法中的默认值。

答案 1 :(得分:1)

这里所需要的只是某种形式的网络服务。您可能希望使用ASP.NET Web API来使用REST Web服务。

不需要单独的进程间通信机制。