mirror of
https://github.com/MTSGJ/opensim.currency.git
synced 2026-07-27 21:10:39 +00:00
async
This commit is contained in:
@@ -1818,8 +1818,8 @@ namespace OpenSim.Modules.Currency
|
|||||||
XmlRpcResponse moneyServResp = null;
|
XmlRpcResponse moneyServResp = null;
|
||||||
try {
|
try {
|
||||||
NSLXmlRpcRequest moneyModuleReq = new NSLXmlRpcRequest(method, arrayParams);
|
NSLXmlRpcRequest moneyModuleReq = new NSLXmlRpcRequest(method, arrayParams);
|
||||||
//moneyServResp = moneyModuleReq.certSend(m_moneyServURL, m_cert, m_checkServerCert, MONEYMODULE_REQUEST_TIMEOUT);
|
|
||||||
moneyServResp = moneyModuleReq.certSend(m_moneyServURL, m_certVerify, m_checkServerCert, MONEYMODULE_REQUEST_TIMEOUT);
|
moneyServResp = moneyModuleReq.certSend(m_moneyServURL, m_certVerify, m_checkServerCert, MONEYMODULE_REQUEST_TIMEOUT);
|
||||||
|
//moneyServResp = moneyModuleReq.certSendAsync(m_moneyServURL, m_certVerify, m_checkServerCert, MONEYMODULE_REQUEST_TIMEOUT).Result;
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
m_log.ErrorFormat("[MONEY MODULE]: genericCurrencyXMLRPCRequest: Unable to connect to Money Server {0}", m_moneyServURL);
|
m_log.ErrorFormat("[MONEY MODULE]: genericCurrencyXMLRPCRequest: Unable to connect to Money Server {0}", m_moneyServURL);
|
||||||
|
|||||||
@@ -1,115 +1,188 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) Contributors, http://www.nsl.tuis.ac.jp
|
* Copyright (c) Contributors, http://www.nsl.tuis.ac.jp
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Net.Http;
|
||||||
using System.Reflection;
|
using System.Text;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Reflection;
|
||||||
using log4net;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using Nwc.XmlRpc;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using System.Net.Security;
|
using log4net;
|
||||||
using NSL.Certificate.Tools;
|
using Nwc.XmlRpc;
|
||||||
|
|
||||||
|
using System.Net.Security;
|
||||||
namespace NSL.Network.XmlRpc
|
using NSL.Certificate.Tools;
|
||||||
{
|
|
||||||
public class NSLXmlRpcRequest : XmlRpcRequest
|
|
||||||
{
|
namespace NSL.Network.XmlRpc
|
||||||
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
{
|
||||||
|
public class NSLXmlRpcRequest : XmlRpcRequest
|
||||||
private Encoding _encoding = new UTF8Encoding();
|
{
|
||||||
private XmlRpcRequestSerializer _serializer = new XmlRpcRequestSerializer();
|
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
||||||
private XmlRpcResponseDeserializer _deserializer = new XmlRpcResponseDeserializer();
|
|
||||||
|
private Encoding _encoding = new UTF8Encoding();
|
||||||
|
private XmlRpcRequestSerializer _serializer = new XmlRpcRequestSerializer();
|
||||||
public NSLXmlRpcRequest()
|
private XmlRpcResponseDeserializer _deserializer = new XmlRpcResponseDeserializer();
|
||||||
{
|
|
||||||
_params = new ArrayList();
|
|
||||||
}
|
public NSLXmlRpcRequest()
|
||||||
|
{
|
||||||
|
_params = new ArrayList();
|
||||||
public NSLXmlRpcRequest(String methodName, IList parameters)
|
}
|
||||||
{
|
|
||||||
MethodName = methodName;
|
|
||||||
_params = parameters;
|
public NSLXmlRpcRequest(String methodName, IList parameters)
|
||||||
}
|
{
|
||||||
|
MethodName = methodName;
|
||||||
|
_params = parameters;
|
||||||
//public XmlRpcResponse certSend(String url, X509Certificate2 myClientCert, bool checkServerCert, Int32 timeout)
|
}
|
||||||
public XmlRpcResponse certSend(String url, NSLCertificateVerify certVerify, bool checkServerCert, Int32 timeout)
|
|
||||||
{
|
|
||||||
m_log.InfoFormat("[MONEY NSL XMLRPC]: XmlRpcResponse certSend: connect to {0}", url);
|
//public XmlRpcResponse certSend(String url, X509Certificate2 myClientCert, bool checkServerCert, Int32 timeout)
|
||||||
|
public XmlRpcResponse certSend(String url, NSLCertificateVerify certVerify, bool checkServerCert, Int32 timeout)
|
||||||
#pragma warning disable SYSLIB0014 // Use HttpClient instead of WebRequest.Create.
|
{
|
||||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
m_log.InfoFormat("[MONEY NSL XMLRPC]: XmlRpcResponse certSend: connect to {0}", url);
|
||||||
#pragma warning restore SYSLIB0014
|
|
||||||
if (request==null) {
|
#pragma warning disable SYSLIB0014 // Use HttpClient instead of WebRequest.Create.
|
||||||
throw new XmlRpcException(XmlRpcErrorCodes.TRANSPORT_ERROR, XmlRpcErrorCodes.TRANSPORT_ERROR_MSG +": Could not create request with " + url);
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||||
}
|
#pragma warning restore SYSLIB0014
|
||||||
|
if (request==null) {
|
||||||
X509Certificate2 clientCert = null;
|
throw new XmlRpcException(XmlRpcErrorCodes.TRANSPORT_ERROR, XmlRpcErrorCodes.TRANSPORT_ERROR_MSG +": Could not create request with " + url);
|
||||||
|
}
|
||||||
request.Method = "POST";
|
|
||||||
request.ContentType = "text/xml";
|
X509Certificate2 clientCert = null;
|
||||||
request.AllowWriteStreamBuffering = true;
|
|
||||||
request.Timeout = timeout;
|
request.Method = "POST";
|
||||||
request.UserAgent = "NSLXmlRpcRequest";
|
request.ContentType = "text/xml";
|
||||||
|
request.AllowWriteStreamBuffering = true;
|
||||||
if (certVerify != null) {
|
request.Timeout = timeout;
|
||||||
clientCert = certVerify.GetPrivateCert();
|
request.UserAgent = "NSLXmlRpcRequest";
|
||||||
if (clientCert != null) request.ClientCertificates.Add(clientCert); // Own certificate // 自身の証明書
|
|
||||||
request.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(certVerify.ValidateServerCertificate);
|
if (certVerify != null) {
|
||||||
}
|
clientCert = certVerify.GetPrivateCert();
|
||||||
else {
|
if (clientCert != null) request.ClientCertificates.Add(clientCert); // Own certificate // 自身の証明書
|
||||||
checkServerCert = false;
|
request.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(certVerify.ValidateServerCertificate);
|
||||||
}
|
}
|
||||||
//
|
else {
|
||||||
if (!checkServerCert) {
|
checkServerCert = false;
|
||||||
request.Headers.Add("NoVerifyCert", "true"); // Do not verify the certificate of the other party // 相手の証明書を検証しない
|
}
|
||||||
}
|
//
|
||||||
|
if (!checkServerCert) {
|
||||||
//
|
request.Headers.Add("NoVerifyCert", "true"); // Do not verify the certificate of the other party // 相手の証明書を検証しない
|
||||||
Stream stream = null;
|
}
|
||||||
try {
|
|
||||||
stream = request.GetRequestStream();
|
//
|
||||||
}
|
Stream stream = null;
|
||||||
#pragma warning disable CS0168
|
try {
|
||||||
catch (Exception ex) {
|
stream = request.GetRequestStream();
|
||||||
#pragma warning restore CS0168
|
}
|
||||||
m_log.ErrorFormat("[MONEY NSL XMLRPC]: GetRequestStream Error: {0}", ex);
|
#pragma warning disable CS0168
|
||||||
stream = null;
|
catch (Exception ex) {
|
||||||
}
|
#pragma warning restore CS0168
|
||||||
if (stream==null) return null;
|
m_log.ErrorFormat("[MONEY NSL XMLRPC]: GetRequestStream Error: {0}", ex);
|
||||||
|
stream = null;
|
||||||
//
|
}
|
||||||
XmlTextWriter xml = new XmlTextWriter(stream, _encoding);
|
if (stream==null) return null;
|
||||||
_serializer.Serialize(xml, this);
|
|
||||||
xml.Flush();
|
//
|
||||||
xml.Close();
|
XmlTextWriter xml = new XmlTextWriter(stream, _encoding);
|
||||||
|
_serializer.Serialize(xml, this);
|
||||||
HttpWebResponse response = null;
|
xml.Flush();
|
||||||
try {
|
xml.Close();
|
||||||
response = (HttpWebResponse)request.GetResponse();
|
|
||||||
}
|
HttpWebResponse response = null;
|
||||||
catch (Exception ex) {
|
try {
|
||||||
m_log.ErrorFormat("[MONEY NSL XMLRPC]: XmlRpcResponse certSend: GetResponse Error: {0}", ex.ToString());
|
response = (HttpWebResponse)request.GetResponse();
|
||||||
}
|
}
|
||||||
StreamReader input = new StreamReader(response.GetResponseStream());
|
catch (Exception ex) {
|
||||||
|
m_log.ErrorFormat("[MONEY NSL XMLRPC]: XmlRpcResponse certSend: GetResponse Error: {0}", ex.ToString());
|
||||||
string inputXml = input.ReadToEnd();
|
}
|
||||||
XmlRpcResponse resp = (XmlRpcResponse)_deserializer.Deserialize(inputXml);
|
StreamReader input = new StreamReader(response.GetResponseStream());
|
||||||
|
|
||||||
input.Close();
|
string inputXml = input.ReadToEnd();
|
||||||
response.Close();
|
XmlRpcResponse resp = (XmlRpcResponse)_deserializer.Deserialize(inputXml);
|
||||||
return resp;
|
|
||||||
}
|
input.Close();
|
||||||
}
|
response.Close();
|
||||||
}
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<XmlRpcResponse> certSendAsync(string url, NSLCertificateVerify certVerify, bool checkServerCert, int timeout)
|
||||||
|
{
|
||||||
|
m_log.InfoFormat("[MONEY NSL XMLRPC]: XmlRpcResponse certSendAsync: connect to {0}", url);
|
||||||
|
|
||||||
|
X509Certificate2 clientCert = null;
|
||||||
|
|
||||||
|
// ハンドラ設定
|
||||||
|
var handler = new HttpClientHandler
|
||||||
|
{
|
||||||
|
ClientCertificateOptions = ClientCertificateOption.Manual,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (certVerify != null)
|
||||||
|
{
|
||||||
|
clientCert = certVerify.GetPrivateCert();
|
||||||
|
if (clientCert != null)
|
||||||
|
{
|
||||||
|
handler.ClientCertificates.Add(clientCert);
|
||||||
|
}
|
||||||
|
|
||||||
|
handler.ServerCertificateCustomValidationCallback = certVerify.ValidateServerCertificate;
|
||||||
|
}
|
||||||
|
else if (!checkServerCert)
|
||||||
|
{
|
||||||
|
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var httpClient = new HttpClient(handler)
|
||||||
|
{
|
||||||
|
Timeout = TimeSpan.FromMilliseconds(timeout)
|
||||||
|
};
|
||||||
|
|
||||||
|
// XML-RPC のシリアライズ
|
||||||
|
string requestXml;
|
||||||
|
using (var stringWriter = new StringWriter())
|
||||||
|
{
|
||||||
|
using var xmlWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented };
|
||||||
|
_serializer.Serialize(xmlWriter, this);
|
||||||
|
xmlWriter.Flush();
|
||||||
|
requestXml = stringWriter.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// リクエストボディの作成
|
||||||
|
var content = new StringContent(requestXml, _encoding, "text/xml");
|
||||||
|
content.Headers.Add("User-Agent", "NSLXmlRpcRequest");
|
||||||
|
|
||||||
|
if (!checkServerCert)
|
||||||
|
{
|
||||||
|
content.Headers.Add("NoVerifyCert", "true");
|
||||||
|
}
|
||||||
|
|
||||||
|
// リクエスト送信
|
||||||
|
HttpResponseMessage response;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
response = await httpClient.PostAsync(url, content);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
m_log.ErrorFormat("[MONEY NSL XMLRPC]: XmlRpcResponse certSendAsync: PostAsync Error: {0}", ex.ToString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// レスポンス取得
|
||||||
|
string responseXml = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
return (XmlRpcResponse)_deserializer.Deserialize(responseXml);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user