public virtual ManagedHttpClientConnection Create(HttpRoute route, ConnectionConfig
                                                          config)
        {
            ConnectionConfig  cconfig              = config != null ? config : ConnectionConfig.Default;
            CharsetDecoder    chardecoder          = null;
            CharsetEncoder    charencoder          = null;
            Encoding          charset              = cconfig.GetCharset();
            CodingErrorAction malformedInputAction = cconfig.GetMalformedInputAction() != null
                                 ? cconfig.GetMalformedInputAction() : CodingErrorAction.Report;

            CodingErrorAction unmappableInputAction = cconfig.GetUnmappableInputAction() != null
                                 ? cconfig.GetUnmappableInputAction() : CodingErrorAction.Report;

            if (charset != null)
            {
                chardecoder = charset.NewDecoder();
                chardecoder.OnMalformedInput(malformedInputAction);
                chardecoder.OnUnmappableCharacter(unmappableInputAction);
                charencoder = charset.NewEncoder();
                charencoder.OnMalformedInput(malformedInputAction);
                charencoder.OnUnmappableCharacter(unmappableInputAction);
            }
            string id = "http-outgoing-" + System.Convert.ToString(Counter.GetAndIncrement());

            return(new LoggingManagedHttpClientConnection(id, log, headerlog, wirelog, cconfig
                                                          .GetBufferSize(), cconfig.GetFragmentSizeHint(), chardecoder, charencoder, cconfig
                                                          .GetMessageConstraints(), null, null, requestWriterFactory, responseParserFactory
                                                          ));
        }
Пример #2
0
        /// <summary>
        /// Converts the provided String to bytes using the
        /// UTF-8 encoding.
        /// </summary>
        /// <remarks>
        /// Converts the provided String to bytes using the
        /// UTF-8 encoding. If <code>replace</code> is true, then
        /// malformed input is replaced with the
        /// substitution character, which is U+FFFD. Otherwise the
        /// method throws a MalformedInputException.
        /// </remarks>
        /// <returns>
        /// ByteBuffer: bytes stores at ByteBuffer.array()
        /// and length is ByteBuffer.limit()
        /// </returns>
        /// <exception cref="CharacterCodingException"/>
        public static ByteBuffer Encode(string @string, bool replace)
        {
            CharsetEncoder encoder = EncoderFactory.Get();

            if (replace)
            {
                encoder.OnMalformedInput(CodingErrorAction.Replace);
                encoder.OnUnmappableCharacter(CodingErrorAction.Replace);
            }
            ByteBuffer bytes = encoder.Encode(CharBuffer.Wrap(@string.ToCharArray()));

            if (replace)
            {
                encoder.OnMalformedInput(CodingErrorAction.Report);
                encoder.OnUnmappableCharacter(CodingErrorAction.Report);
            }
            return(bytes);
        }