call subroutines via URL across local network?

2019-01-12 12:40发布

问题:

I'm writing a program in Visual Basic .NET to perform actions on a PC on my local network.

I'm looking basically for some sort of extremely simple solution to allow me to:

  • call subroutines/functions from other machines on the network via their web browsers
  • post arguments into the url if possible (see optimistic example below)
  • allow a response string of html to be sent (see example again)

Example: entering http://192.168.1.72/function/argument1/argument2 would run the 'function' sub/function within my winforms application application on that (locally networked) machine, and pass through argument1 and argument2 (if they were included), then return a response page/string to the requesting browser as feedback

Could anyone point me in the way of a way to do this? I'm looking for something fairly simple, but reliable because it may end up running around the clock

回答1:

I'm looking for something fairly simple, but reliable because it may end up running around the clock

I think simpliest way would be using the WebServiceHost class of WCF:

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

Put this lines to formload(or any other entry point) of your application,

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

and call like http://192.168.1.72/Myservice/function/a/b from your browser. That is all.

----Full Working Code----

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
    }
}

EDIT

is it possible to determine the IP address the request is coming from?

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

var address = m.Address;

any way to make the {arguments} optional?

Just remove the UriTemplate parameter from WebGet attribute. Then your url will be as

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

If you want to make argument2 optional for ex, call as

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

and argument2 will get the default value in your method.



回答2:

All you need here is some form of web service. You may want to use a REST web service using ASP.NET Web API.

A separate Inter-process communication mechanism would not be necessary.