gpt4 book ai didi

c# - 通过 HTTPS 实现 C# XMLRPC.NET 客户端和服务器

转载 作者:太空狗 更新时间:2023-10-29 21:29:45 25 4
gpt4 key购买 nike

很难找到有关与 https 一起使用的 XMLRPC.net 库的信息。

唯一可以设置“https”URL 的文档在此处:http://xml-rpc.net/faq/xmlrpcnetfaq-2-5-0.html#2.3但它并没有准确解释如何正确设置。

在下载中提供的示例基础上进行实验 http://xmlrpcnet.googlecode.com/files/xml-rpc.net.2.5.0.zip我试过这个:

StateNameServer 解决方案的 client.cs 文件中的更改:

IStateName svr = (IStateName)Activator.GetObject(
typeof(IStateName), "https://localhost:5678/statename.rem");

服务器代码是什么样的

    IDictionary props = new Hashtable();
props["name"] = "MyHttpChannel";
props["port"] = 5678;
HttpChannel channel = new HttpChannel(
props,
null,
new XmlRpcServerFormatterSinkProvider()
);

ChannelServices.RegisterChannel(channel, false);

RemotingConfiguration.RegisterWellKnownServiceType(
typeof(StateNameServer),
"statename.rem",
WellKnownObjectMode.Singleton);

客户端在尝试使用 HTTPS 联系服务器时显然会抛出一个异常,因为我不知道如何配置它。无论如何有人可以帮忙吗?我应该寻找什么样的东西?

非常感谢!

最佳答案

首先,我要衷心感谢 Charles Cook 在这个问题上的帮助以及对 XMLRPC.NET 的开发。

其次,此示例基于可在此处下载的 XMLRPC.NET StateNameServer 示例: http://xml-rpc.net/download.html

所以这里是解决方案:

<强>1。生成或获取 [自签名] 证书(例如使用 makecert.exe)

<强>2。将此证书添加到您的服务器配置,并使用 httpcfg.exe 或其他工具(如 HttpSysConfig)指定要用于 XMLRPC.NET 服务器的端口(在本例中为 5678)。 (开源)

<强>3。使用以下代码实现您的 XMLRPC.NET 服务器:

using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

using CookComputing.XmlRpc;

using System.Net;
using System.IO;

public class _
{
static void Main(string[] args)
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("https://127.0.0.1:5678/");
listener.Start();
while (true)
{
HttpListenerContext context = listener.GetContext();
ListenerService svc = new StateNameService();
svc.ProcessRequest(context);
}

Console.WriteLine("Press <ENTER> to shutdown");
Console.ReadLine();
}
}

public class StateNameService : ListenerService
{
[XmlRpcMethod("examples.getStateName")]
public string GetStateName(int stateNumber)
{
if (stateNumber < 1 || stateNumber > m_stateNames.Length)
throw new XmlRpcFaultException(1, "Invalid state number");
return m_stateNames[stateNumber - 1];
}

string[] m_stateNames
= { "Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Lousiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississipi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylviania", "Rhose Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming" };
}

public abstract class ListenerService : XmlRpcHttpServerProtocol
{
public virtual void ProcessRequest(HttpListenerContext RequestContext)
{
try
{
IHttpRequest req = new ListenerRequest(RequestContext.Request);
IHttpResponse resp = new ListenerResponse(RequestContext.Response);
HandleHttpRequest(req, resp);
RequestContext.Response.OutputStream.Close();
}
catch (Exception ex)
{
// "Internal server error"
RequestContext.Response.StatusCode = 500;
RequestContext.Response.StatusDescription = ex.Message;
}
}
}

public class ListenerRequest : CookComputing.XmlRpc.IHttpRequest
{
public ListenerRequest(HttpListenerRequest request)
{
this.request = request;
}

public Stream InputStream
{
get { return request.InputStream; }
}

public string HttpMethod
{
get { return request.HttpMethod; }
}

private HttpListenerRequest request;
}

public class ListenerResponse : CookComputing.XmlRpc.IHttpResponse
{
public ListenerResponse(HttpListenerResponse response)
{
this.response = response;
}

string IHttpResponse.ContentType
{
get { return response.ContentType; }
set { response.ContentType = value; }
}

TextWriter IHttpResponse.Output
{
get { return new StreamWriter(response.OutputStream); }
}

Stream IHttpResponse.OutputStream
{
get { return response.OutputStream; }
}

int IHttpResponse.StatusCode
{
get { return response.StatusCode; }
set { response.StatusCode = value; }
}

string IHttpResponse.StatusDescription
{
get { return response.StatusDescription; }
set { response.StatusDescription = value; }
}

private HttpListenerResponse response;
}

public class StateNameServer : MarshalByRefObject, IStateName
{
public string GetStateName(int stateNumber)
{
if (stateNumber < 1 || stateNumber > m_stateNames.Length)
throw new XmlRpcFaultException(1, "Invalid state number");
return m_stateNames[stateNumber-1];
}

public string GetStateNames(StateStructRequest request)
{
if (request.state1 < 1 || request.state1 > m_stateNames.Length)
throw new XmlRpcFaultException(1, "State number 1 invalid");
if (request.state2 < 1 || request.state2 > m_stateNames.Length)
throw new XmlRpcFaultException(1, "State number 1 invalid");
if (request.state3 < 1 || request.state3 > m_stateNames.Length)
throw new XmlRpcFaultException(1, "State number 1 invalid");
string ret = m_stateNames[request.state1-1] + " "
+ m_stateNames[request.state2-1] + " "
+ m_stateNames[request.state3-1];
return ret;
}

string[] m_stateNames
= { "Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Lousiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississipi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylviania", "Rhose Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming" };
}

<强>4。使用以下代码实现您的 XMLRPC.NET 客户端(该代码还会创建一个新的 X509 客户端证书)

using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

using CookComputing.XmlRpc;
using System.Net;
using System.Security.Cryptography.X509Certificates;

class _
{
public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
{
public TrustAllCertificatePolicy() { }
public bool CheckValidationResult(ServicePoint sp,
X509Certificate cert,
WebRequest req,
int problem)
{
return true;
}
}
static void Main(string[] args)
{
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
IStateName proxy = XmlRpcProxyGen.Create<IStateName>();
XmlRpcClientProtocol cp = (XmlRpcClientProtocol)proxy;
cp.Url = "https://127.0.0.1:5678/";
cp.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate(@"C:\path\to\your\certificate\file\my.cer"));
cp.KeepAlive = false;
//cp.Expect100Continue = false;
//cp.NonStandard = XmlRpcNonStandard.All;

string stateName = ((IStateName)cp).GetStateName(13);
}
}

当然,我不会在这里给出 ServerStateName 的接口(interface)实现,但您可以使用顶部的下载链接在示例文件中找到它。

备注:

System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();将允许服务器实现接受您自己生成的自签名证书。我认为这对于证书颁发机构颁发的证书是没有必要的。

如果您发现任何可以改进和不正确的地方,我们将不胜感激。

关于c# - 通过 HTTPS 实现 C# XMLRPC.NET 客户端和服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7904592/

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