Example #1
0
        /**
         * Standard post without multipart but already support on Factory (memory management)
         *
         * @return the list of HttpData object (attribute and file) to be reused on next post
         */
        private static async Task <List <IInterfaceHttpData> > FormpostAsync(Bootstrap bootstrap,
                                                                             Uri uriSimple, FileStream file, IHttpDataFactory factory,
                                                                             IList <HeaderEntry <AsciiString, ICharSequence> > headers)
        {
            // XXX /formpost
            // Start the connection attempt.
            IChannel channel = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port));

            // Prepare the HTTP request.
            IHttpRequest request = new DefaultHttpRequest(DotNetty.Codecs.Http.HttpVersion.Http11, HttpMethod.Post, uriSimple.ToString());

            // Use the PostBody encoder
            HttpPostRequestEncoder bodyRequestEncoder =
                new HttpPostRequestEncoder(factory, request, false);      // false => not multipart

            // it is legal to add directly header or cookie into the request until finalize
            foreach (var entry in headers)
            {
                request.Headers.Set(entry.Key, entry.Value);
            }

            // add Form attribute
            bodyRequestEncoder.AddBodyAttribute("getform", "POST");
            bodyRequestEncoder.AddBodyAttribute("info", "first value");
            bodyRequestEncoder.AddBodyAttribute("secondinfo", "secondvalue ���&");
            bodyRequestEncoder.AddBodyAttribute("thirdinfo", TextArea);
            bodyRequestEncoder.AddBodyAttribute("fourthinfo", TextAreaLong);
            bodyRequestEncoder.AddBodyFileUpload("myfile", file, "application/x-zip-compressed", false);

            // finalize request
            request = bodyRequestEncoder.FinalizeRequest();

            // Create the bodylist to be reused on the last version with Multipart support
            var bodylist = bodyRequestEncoder.GetBodyListAttributes();

            var list = new List <object>();

            // send request
            list.Add(request);

            // test if request was chunked and if so, finish the write
            if (bodyRequestEncoder.IsChunked)
            { // could do either request.isChunked()
              // either do it through ChunkedWriteHandler
                list.Add(bodyRequestEncoder);
            }
            await channel.WriteAndFlushManyAsync(list);

            // Do not clear here since we will reuse the InterfaceHttpData on the next request
            // for the example (limit action on client side). Take this as a broadcast of the same
            // request on both Post actions.
            //
            // On standard program, it is clearly recommended to clean all files after each request
            // bodyRequestEncoder.cleanFiles();

            // Wait for the server to close the connection.
            await channel.CloseCompletion;

            return(bodylist);
        }