gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 11:34:05 27 4
gpt4 key购买 nike

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

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

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

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

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

最佳答案

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

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

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

将此行放入您的应用程序的表单加载(或任何其他入口点),

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

编辑

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?

只需从 WebGet 属性中删除 UriTemplate 参数。那么你的网址将是

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

如果你想让 argument2 对于 ex 是可选的,调用 as

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

argument2 将在您的方法中获取默认值。

关于.net - 通过本地网络的 URL 调用子程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15852859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com