Exemple #1
0
 public override string ToRawString(Direction direction)
 {
     if (direction == Direction)
     {
         StringBuilder sb = new StringBuilder(4096);
         if (m_headers != null)
         {
             if (m_headers.ContainsKey("method"))
             {
                 sb.Append(m_headers["method"]);
                 sb.Append(": ");
                 sb.Append(Host);
                 sb.AppendLine();
             }
             m_headers.Remove("host");
             foreach (KeyValuePair <string, string> kvp in m_headers)
             {
                 if (kvp.Key == "method")
                 {
                     continue;
                 }
                 sb.Append(kvp.Key);
                 sb.Append(' ');
                 sb.Append(kvp.Value);
                 sb.AppendLine();
             }
             sb.AppendLine();
         }
         using (MemoryStream ms = new MemoryStream(1024))
         {
             using (XmlTextWriter xml = new XmlTextWriter(ms, new UTF8Encoding(false)))
             {
                 if (Data is XmlRpcResponse)
                 {
                     var xrpcr = new XmlRpcResponseSerializer();
                     xrpcr.Serialize(xml, Data);
                 }
                 else
                 {
                     var xrpcr = new XmlRpcRequestSerializer();
                     xrpcr.Serialize(xml, Data);
                 }
                 xml.Flush();
                 sb.Append(UTF8Encoding.UTF8.GetString(ms.ToArray()));
             }
         }
         return(sb.ToString());
     }
     else
     {
         return(string.Empty);
     }
 }
        public XmlRpcResponse certSend(String url, X509Certificate2 clientCert, bool checkServerCert, Int32 timeout)
        {
            m_log.InfoFormat("[MONEY NSL RPC]: XmlRpcResponse certSend: connect to {0}", url);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            if (request == null)
            {
                throw new XmlRpcException(XmlRpcErrorCodes.TRANSPORT_ERROR, XmlRpcErrorCodes.TRANSPORT_ERROR_MSG + ": Could not create request with " + url);
            }

            request.Method      = "POST";
            request.ContentType = "text/xml";
            request.AllowWriteStreamBuffering = true;
            request.Timeout   = timeout;
            request.UserAgent = "NSLXmlRpcRequest";

            if (clientCert != null)
            {
                request.ClientCertificates.Add(clientCert);                                     // 自身の証明書
            }
            if (!checkServerCert)
            {
                request.Headers.Add("NoVerifyCert", "true");                                    // 相手の証明書を検証しない
            }
            Stream        stream = request.GetRequestStream();
            XmlTextWriter xml    = new XmlTextWriter(stream, _encoding);

            _serializer.Serialize(xml, this);
            xml.Flush();
            xml.Close();

            HttpWebResponse response = null;

            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            try {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception ex) {
                m_log.ErrorFormat("[MONEY NSL RPC]: XmlRpcResponse certSend: GetResponse Error: {0}", ex);
            }
            StreamReader input = new StreamReader(response.GetResponseStream());

            string         inputXml = input.ReadToEnd();
            XmlRpcResponse resp     = (XmlRpcResponse)_deserializer.Deserialize(inputXml);

            input.Close();
            response.Close();
            return(resp);
        }
Exemple #3
0
        /// <summary>
        ///   Send the request to the server.
        /// </summary>
        /// <param name = "url"><c>String</c> The url of the XML-RPC server.</param>
        /// <returns><c>XmlRpcResponse</c> The response generated.</returns>
        public XmlRpcResponse Send(String url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            if (request == null)
            {
                throw new XmlRpcException(XmlRpcErrorCodes.TRANSPORT_ERROR,
                                          XmlRpcErrorCodes.TRANSPORT_ERROR_MSG + ": Could not create request with " +
                                          url);
            }
            request.Method      = "POST";
            request.ContentType = "text/xml";
            request.AllowWriteStreamBuffering = true;
            request.KeepAlive = !_disableKeepAlive;

            Stream        stream = request.GetRequestStream();
            XmlTextWriter xml    = new XmlTextWriter(stream, _encoding);

            _serializer.Serialize(xml, this);
            xml.Flush();
            xml.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader    input    = new StreamReader(response.GetResponseStream());

            string         inputXml = input.ReadToEnd();
            XmlRpcResponse resp;

            try
            {
                resp = (XmlRpcResponse)_deserializer.Deserialize(inputXml);
            }
            catch (Exception)
            {
                RequestResponse = inputXml;
                throw;
            }
            input.Close();
            response.Close();
            return(resp);
        }
Exemple #4
0
        public XmlRpcResponse certSend(String url, X509Certificate2 cert, bool checkCert, Int32 timeout)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            if (request == null)
            {
                throw new XmlRpcException(XmlRpcErrorCodes.TRANSPORT_ERROR,
                                          XmlRpcErrorCodes.TRANSPORT_ERROR_MSG + ": Could not create request with " + url);
            }

            request.Method      = "POST";
            request.ContentType = "text/xml";
            request.AllowWriteStreamBuffering = true;
            request.Timeout = timeout;

            if (cert != null)
            {
                request.ClientCertificates.Add(cert);
            }
            if (!checkCert)
            {
                request.Headers.Add("NoVerifyCert", "true");
            }

            Stream        stream = request.GetRequestStream();
            XmlTextWriter xml    = new XmlTextWriter(stream, _encoding);

            _serializer.Serialize(xml, this);
            xml.Flush();
            xml.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader    input    = new StreamReader(response.GetResponseStream());

            XmlRpcResponse resp = (XmlRpcResponse)_deserializer.Deserialize(input);

            input.Close();
            response.Close();
            return(resp);
        }
        //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);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            if (request == null)
            {
                throw new XmlRpcException(XmlRpcErrorCodes.TRANSPORT_ERROR, XmlRpcErrorCodes.TRANSPORT_ERROR_MSG + ": Could not create request with " + url);
            }

            X509Certificate2 clientCert = null;

            request.Method      = "POST";
            request.ContentType = "text/xml";
            request.AllowWriteStreamBuffering = true;
            request.Timeout   = timeout;
            request.UserAgent = "NSLXmlRpcRequest";

            if (certVerify != null)
            {
                clientCert = certVerify.GetPrivateCert();
                if (clientCert != null)
                {
                    request.ClientCertificates.Add(clientCert);                      // Own certificate   // 自身の証明書
                }
                request.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(certVerify.ValidateServerCertificate);
            }
            else
            {
                checkServerCert = false;
            }
            //
            if (!checkServerCert)
            {
                request.Headers.Add("NoVerifyCert", "true");   // Do not verify the certificate of the other party  // 相手の証明書を検証しない
            }

            //
            Stream stream = null;

            try {
                stream = request.GetRequestStream();
            }
#pragma warning disable CS0168
            catch (Exception ex) {
#pragma warning restore CS0168
                m_log.ErrorFormat("[MONEY NSL XMLRPC]: GetRequestStream Error: {0}", ex);
                stream = null;
            }
            if (stream == null)
            {
                return(null);
            }

            //
            XmlTextWriter xml = new XmlTextWriter(stream, _encoding);
            _serializer.Serialize(xml, this);
            xml.Flush();
            xml.Close();

            HttpWebResponse response = null;
            try {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception ex) {
                m_log.ErrorFormat("[MONEY NSL XMLRPC]: XmlRpcResponse certSend: GetResponse Error: {0}", ex.ToString());
            }
            StreamReader input = new StreamReader(response.GetResponseStream());

            string         inputXml = input.ReadToEnd();
            XmlRpcResponse resp     = (XmlRpcResponse)_deserializer.Deserialize(inputXml);

            input.Close();
            response.Close();
            return(resp);
        }