System.Uri.EscapeDataString C# (CSharp) Method

EscapeDataString() public static method

public static EscapeDataString ( string stringToEscape ) : string
stringToEscape string
return string
        public static string EscapeDataString(string stringToEscape)
        {
            if ((object)stringToEscape == null)
                throw new ArgumentNullException(nameof(stringToEscape));

            if (stringToEscape.Length == 0)
                return string.Empty;

            int position = 0;
            char[] dest = UriHelper.EscapeString(stringToEscape, 0, stringToEscape.Length, null, ref position, false,
                c_DummyChar, c_DummyChar, c_DummyChar);
            if (dest == null)
                return stringToEscape;
            return new string(dest, 0, position);
        }

Usage Example

Example #1
0
        private void Configure()
        {
            _authToken         = Robot.GetConfigVariable("MMBOT_HIPCHAT_AUTHTOKEN");
            _apiMessageRoomUrl = new Uri(string.Format(@"https://api.hipchat.com/v1/rooms/message?format=json&auth_token={0}", Uri.EscapeDataString(_authToken)));
            _host     = Robot.GetConfigVariable("MMBOT_HIPCHAT_HOST") ?? "chat.hipchat.com";
            _confhost = Robot.GetConfigVariable("MMBOT_HIPCHAT_CONFHOST") ?? "conf.hipchat.com";
            _nick     = Robot.GetConfigVariable("MMBOT_HIPCHAT_NICK");
            _roomNick = Robot.GetConfigVariable("MMBOT_HIPCHAT_ROOMNICK");
            _username = Robot.GetConfigVariable("MMBOT_HIPCHAT_USERNAME");
            _password = Robot.GetConfigVariable("MMBOT_HIPCHAT_PASSWORD");
            _rooms    = (Robot.GetConfigVariable("MMBOT_HIPCHAT_ROOMS") ?? string.Empty)
                        .Trim()
                        .Split(',')
                        .Select(s => s.Trim())
                        .Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
            _logRooms = (Robot.GetConfigVariable("MMBOT_HIPCHAT_LOGROOMS") ?? string.Empty)
                        .Trim()
                        .Split(',')
                        .Select(s => s.Trim())
                        .Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();

            if (_host == null || _nick == null | _password == null || !_rooms.Any())
            {
                var helpSb = new StringBuilder();
                helpSb.AppendLine("The HipCat adapter is not configured correctly and hence will not be enabled.");
                helpSb.AppendLine("To configure the HipChat adapter, please set the following configuration properties:");
                helpSb.AppendLine("  MMBOT_HIPCHAT_HOST: The host name defaults to chat.hipchat.com");
                helpSb.AppendLine("  MMBOT_HIPCHAT_CONFHOST: The host name defaults to conf.hipchat.com");
                helpSb.AppendLine("  MMBOT_HIPCHAT_NICK: The nick name of the bot account on HipChat, e.g. mmbot");
                helpSb.AppendLine("  MMBOT_HIPCHAT_ROOMNICK: The name of the bot account on HipChat, e.g. mmbot Bot");
                helpSb.AppendLine("  MMBOT_HIPCHAT_USERNAME: The username of the bot account on HipChat, e.g. 70126_494074");
                helpSb.AppendLine("  MMBOT_HIPCHAT_PASSWORD: The password of the bot account on HipChat");
                helpSb.AppendLine("  MMBOT_HIPCHAT_ROOMS: A comma separated list of room names that mmbot should join");
                helpSb.AppendLine("  MMBOT_HIPCHAT_AUTHTOKEN: An optional authorization token (for v1 API) that if set will allow room messages to use hip-chat specific formatting features.");
                helpSb.AppendLine("More info on these values and how to create the mmbot.ini file can be found at https://github.com/mmbot/mmbot/wiki/Configuring-mmbot");
                Logger.Warn(helpSb.ToString());
                _isConfigured = false;
            }
            else
            {
                _isConfigured = true;
            }
        }