通过本地网络的 URL 调用子程序? [英] call subroutines via URL across local network?
问题描述
我正在用 Visual Basic .NET 编写程序,以便在本地网络上的 PC 上执行操作.
我基本上正在寻找某种极其简单的解决方案来让我:
- 通过网络浏览器从网络上的其他机器调用子程序/函数
- 如果可能,将参数发布到 url 中(参见下面的乐观示例)
- 允许发送 html 响应字符串(再次参见示例)
示例:输入 http://192.168.1.72/function/argument1/argument2
将在该(本地联网)机器上运行我的 winforms 应用程序中的函数"子/函数,并通过参数 1 和参数 2(如果包含它们),则将响应页面/字符串作为反馈返回给请求浏览器
有人能指出我这样做的方法吗?我正在寻找一些相当简单但可靠的东西,因为它最终可能会全天候运行
我正在寻找一些相当简单但可靠的东西,因为它可能会全天候运行
我认为最简单的方法是使用 WebServiceHost WCF 类:
[ServiceContract]公共类 MyService{[OperationContract, WebGet(UriTemplate = "/function/{argument1}/{argument2}")]公共字符串AMethod(字符串参数1,字符串参数2){返回参数 1 + " " + 参数 2;}}
将此行放在应用程序的 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
.仅此而已.
----完整的工作代码----
使用系统;使用 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(){初始化组件();}protected override void OnLoad(EventArgs e){var wsh = new WebServiceHost(typeof(MyService), new Uri("http://0.0.0.0/MyService"));wsh.Open();}}[服务合约]公共类 MyService{[OperationContract, WebGet(UriTemplate = "/function/{argument1}/{argument2}")]公共字符串AMethod(字符串参数1,字符串参数2){返回参数 1 + " " + 参数 2;}///******** 编辑 ********///[OperationContract, WebGet(UriTemplate = "/function2/{argument1}/{argument2}")]公共流 F2(字符串参数 1,字符串参数 2){return ToHtml("<html><h1>" + argument1 + " " + argument2 + "</h1></HtmlDocument>");}静态流 ToHtml(字符串结果){var data = Encoding.UTF8.GetBytes(result);WebOperationContext.Current.OutgoingResponse.ContentType = "text/html; charset=utf-8";WebOperationContext.Current.OutgoingResponse.ContentLength = data.Length;返回新的内存流(数据);}///编辑结束}}
编辑
<块引用>是否可以确定请求来自的 IP 地址?
var m = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] 作为 RemoteEndpointMessageProperty;var address = m.Address;
<块引用>
有什么办法可以让 {arguments} 成为可选的?
只需从 WebGet
属性中删除 UriTemplate
参数即可.那么你的网址将是
http://1192.168.1.72/MyService/AMethod?argument1=aaaa&argument2=bbb
如果你想为 ex 设置参数 2 可选,调用 as
http://1192.168.1.72/MyService/AMethod?argument1=aaaa
argument2 将在您的方法中获得默认值.
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屋!