private static Stream GetStreamForUrl(string url, string pageUrl, IHTMLElement element)
        {
            if (UrlHelper.IsFileUrl(url))
            {
                string path = new Uri(url).LocalPath;
                if (File.Exists(path))
                {
                    return(File.OpenRead(path));
                }
                else
                {
                    if (ApplicationDiagnostics.AutomationMode)
                    {
                        Trace.WriteLine("File " + url + " not found");
                    }
                    else
                    {
                        Trace.Fail("File " + url + " not found");
                    }
                    return(null);
                }
            }
            else if (UrlHelper.IsUrlDownloadable(url))
            {
                return(HttpRequestHelper.SafeDownloadFile(url));
            }
            else
            {
                string baseUrl = HTMLDocumentHelper.GetBaseUrlFromDocument((IHTMLDocument2)element.document) ?? pageUrl ?? ((IHTMLDocument2)element.document).url;
                if (baseUrl == null)
                {
                    return(null);
                }

                url = UrlHelper.EscapeRelativeURL(baseUrl, url);
                if (UrlHelper.IsUrlDownloadable(url))
                {
                    return(HttpRequestHelper.SafeDownloadFile(url));
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Call an XmlRpc method
        /// </summary>
        /// <param name="methodName">method name</param>
        /// <param name="parameters">variabile length list of parameters</param>
        /// <returns>response from the remote host</returns>
        /// <exception cref="Exception">allows all exceptions to propogate out of the method</exception>
        public async Task <XmlRpcMethodResponse> CallMethod(string methodName, params XmlRpcValue[] parameters)
        {
            //select the encoding
            Encoding encodingToUse = new UTF8Encoding(false, false);

            try
            {
                encodingToUse = Encoding.GetEncoding(_transportEncoding);
            }
            catch (Exception)
            {
            }

            // build the XmlRpc packet
            var requestBytes = GetRequestString(encodingToUse, methodName, parameters, false);

            if (ApplicationDiagnostics.VerboseLogging)
            {
                LogXmlRpcRequest(encodingToUse, methodName, parameters);
            }

            // send the request
            HttpResponseMessage response;

            try
            {
                response = await HttpRequestHelper.SendRequest(_hostname, async delegate(HttpRequestMessage request)
                {
                    request.Method = new HttpMethod("POST");
                    //request.AllowAutoRedirect = false;

                    if (_requestFilter != null)
                    {
                        await _requestFilter(request);
                    }

                    request.Content = new HttpStringContent(requestBytes, UnicodeEncoding.Utf8, encodingToUse.WebName);

                    ////request = String.Format(CultureInfo.InvariantCulture, "{0};charset={1}", MimeHelper.TEXT_XML, encodingToUse.WebName);

                    //using (Stream requestStream = await request.GetRequestStreamAsync())
                    //{
                    //    await StreamHelper.TransferAsync(new MemoryStream(requestBytes), requestStream);
                    //    await requestStream.FlushAsync();
                    //}
                });
            }
            catch
            {
                if (!ApplicationDiagnostics.VerboseLogging)  // if test mode, request has already been logged
                {
                    LogXmlRpcRequest(encodingToUse, methodName, parameters);
                }

                throw;
            }

            //// WinLive 616: The response encoding may not necessarily be the same as our request encoding. Attempt to
            //// use the encoding specified in the HTTP header.
            //string characterSet;
            //if (TryGetCharacterSet(response, out characterSet))
            //{
            //    encodingToUse = StringHelper.GetEncoding(characterSet, encodingToUse);
            //}

            ////// return the response
            //var stream = response.GetResponseStream();
            //if (response.Headers[HttpRequestHeader.ContentEncoding] != null)
            //{
            //    if (response.Headers[HttpRequestHeader.ContentEncoding].ToLowerInvariant().Contains("gzip"))
            //    {
            //        stream = new GZipStream(stream, CompressionMode.Decompress);
            //    }
            //}

            //response.Content.ReadAsStringAsync();

            //using (StreamReader reader = new StreamReader(stream, encodingToUse))
            //{
            string xmlRpcString = await response.Content.ReadAsStringAsync();

            if (ApplicationDiagnostics.VerboseLogging)
            {
                LogXmlRpcResponse(xmlRpcString);
            }

            try
            {
                XmlRpcMethodResponse xmlRpcResponse = new XmlRpcMethodResponse(xmlRpcString);
                if (xmlRpcResponse.FaultOccurred)
                {
                    if (!ApplicationDiagnostics.VerboseLogging)      // if test mode, response has already been logged
                    {
                        LogXmlRpcRequest(encodingToUse, methodName, parameters);
                        LogXmlRpcResponse(xmlRpcString);
                    }
                }
                return(xmlRpcResponse);
            }
            catch (Exception ex)
            {
                //Debug.WriteLine("Exception parsing XML-RPC response:\r\n\r\n" + ex.ToString() + "\r\n\r\n" + xmlRpcString);
                throw;
            }
            // }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Call an XmlRpc method
        /// </summary>
        /// <param name="methodName">method name</param>
        /// <param name="parameters">variabile length list of parameters</param>
        /// <returns>response from the remote host</returns>
        /// <exception cref="Exception">allows all exceptions to propogate out of the method</exception>
        public XmlRpcMethodResponse CallMethod(string methodName, params XmlRpcValue[] parameters)
        {
            //select the encoding
            Encoding encodingToUse = StringHelper.GetEncoding(_transportEncoding, new UTF8Encoding(false, false));

            // build the XmlRpc packet
            byte[] requestBytes = GetRequestBytes(encodingToUse, methodName, parameters, false);

            if (ApplicationDiagnostics.VerboseLogging)
            {
                LogXmlRpcRequest(encodingToUse, methodName, parameters);
            }

            // send the request
            HttpWebResponse response;

            try
            {
                response = HttpRequestHelper.SendRequest(_hostname, delegate(HttpWebRequest request)
                {
                    request.Method            = "POST";
                    request.AllowAutoRedirect = false;
                    request.ContentType       = String.Format(CultureInfo.InvariantCulture, "{0};charset={1}", MimeHelper.TEXT_XML, encodingToUse.WebName);
                    if (_requestFilter != null)
                    {
                        _requestFilter(request);
                    }
                    using (Stream requestStream = request.GetRequestStream())
                        StreamHelper.Transfer(new MemoryStream(requestBytes), requestStream);
                });
            }
            catch
            {
                if (!ApplicationDiagnostics.VerboseLogging)  // if test mode, request has already been logged
                {
                    LogXmlRpcRequest(encodingToUse, methodName, parameters);
                }

                throw;
            }

            // WinLive 616: The response encoding may not necessarily be the same as our request encoding. Attempt to
            // use the encoding specified in the HTTP header.
            string characterSet;

            if (TryGetCharacterSet(response, out characterSet))
            {
                encodingToUse = StringHelper.GetEncoding(characterSet, encodingToUse);
            }

            // return the response
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), encodingToUse))
            {
                string xmlRpcString = reader.ReadToEnd();

                if (ApplicationDiagnostics.VerboseLogging)
                {
                    LogXmlRpcResponse(xmlRpcString);
                }

                try
                {
                    XmlRpcMethodResponse xmlRpcResponse = new XmlRpcMethodResponse(xmlRpcString);
                    if (xmlRpcResponse.FaultOccurred)
                    {
                        if (!ApplicationDiagnostics.VerboseLogging)  // if test mode, response has already been logged
                        {
                            LogXmlRpcRequest(encodingToUse, methodName, parameters);
                            LogXmlRpcResponse(xmlRpcString);
                        }
                    }
                    return(xmlRpcResponse);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("Exception parsing XML-RPC response:\r\n\r\n" + ex.ToString() + "\r\n\r\n" + xmlRpcString);
                    throw;
                }
            }
        }
Ejemplo n.º 4
0
 private void Filter(HttpWebRequest request)
 {
     request.Credentials = HttpRequestHelper.CreateHttpCredentials(_username, _password, _url, _digestOnly);
 }