在整个本地网络通过URL调用子程序? [英] call subroutines via URL across local network?

查看:301
本文介绍了在整个本地网络通过URL调用子程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在Visual Basic .NET程序,以我的本地网络上执行的PC上的操作。

我在找基本上是某种极其简单的解决方案,让我:

  • 在子程序调用/通过网络浏览器从网络上的其他机器功能
  • 后的参数到URL如果可能的话(见下文乐观例子)
  • 允许HTML的响应字符串发送(见例如再次)

示例: 进入 http://192.168.1.72/function/argument1/argument2 将运行功能分了(本地网络)的机器在我的WinForms应用程序的应用程序内/功能,通过参数1和参数2(如果他们被包括在内),然后返回响应页/串到请求浏览器作为反馈

任何人都可以点我的方式做到这一点的方法吗?我正在寻找的东西很简单,但可靠的,因为它最终可能会全天候运行

解决方案
  

我在找的东西很简单,但可靠的,因为它最终可能会全天候运行

我觉得simpliest方法是使用 WebServiceHost 类WCF的:

  [的ServiceContract]
公共类为MyService
{
    [OperationContract的,WebGet(UriTemplate =/功能/ {参数1} / {参数2})]
    公共字符串amethod方法(字符串参数1,字符串参数2)
    {
        返回自变量1 ++参数2;
    }
}
 

将这个线formload(或任何其他入口点),您的应用程序,

  VAR WSH =新WebServiceHost(typeof运算(为MyService),新的URI(http://0.0.0.0/MyService));
wsh.Open();
 

和调用像 http://192.168.1.72/Myservice/function/a/b 从您的浏览器。这就是一切。

----全部工作code ----

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms的;

命名空间WindowsFormsApplication1
{
    公共部分类Form1中:形态
    {
        公共Form1中()
        {
            的InitializeComponent();
        }

        保护覆盖无效的OnLoad(EventArgs的发送)
        {
            VAR WSH =新WebServiceHost(typeof运算(为MyService),新的URI(http://0.0.0.0/MyService));
            wsh.Open();
        }
    }

    [的ServiceContract]
    公共类为MyService
    {
        [OperationContract的,WebGet(UriTemplate =/功能/ {参数1} / {参数2})]
        公共字符串amethod方法(字符串参数1,字符串参数2)
        {
            返回自变量1 ++参数2;
        }

        ///******** 编辑 ********
        ///
        [OperationContract的,WebGet(UriTemplate =/函数2 / {参数1} / {参数2})]
        公共流F2(字符串参数1,字符串参数2)
        {
            返回ToHtml(< HTML>< H1>中+参数1 ++参数2 +< / H1>< /的HTMLDocument>);
        }

        静流ToHtml(字符串结果)
        {
            VAR数据= Encoding.UTF8.GetBytes(结果);

            WebOperationContext.Current.OutgoingResponse.ContentType =text / html的;字符集= UTF-8;
            WebOperationContext.Current.OutgoingResponse.ContentLength = data.Length;

            返回新的MemoryStream(数据);
        }
        /// END编辑
    }
}
 

修改

  

是,能够确定该请求是来自?到来的IP地址

 变种M = OperationContext.Current
        .IncomingMessageProperties [RemoteEndpointMessageProperty.Name]作为RemoteEndpointMessageProperty;

VAR地址= m.Address;
 

  

的任何方法,使{}参数可选?

只需删除 WebGet 属性 UriTemplate 参数。那么你的URL将被作为

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

如果你想参数2可选的前,叫为

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

和参数2会在你的方法的默认值。

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

解决方案

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.

这篇关于在整个本地网络通过URL调用子程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆