/** * Multipart example */ private static async Task FormpostmultipartAsync( Bootstrap bootstrap, Uri uriFile, IHttpDataFactory factory, IList <HeaderEntry <AsciiString, ICharSequence> > headers, List <IInterfaceHttpData> bodylist) { // XXX /formpostmultipart // Start the connection attempt. IChannel channel = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port)); // Prepare the HTTP request. var request = new DefaultHttpRequest(DotNetty.Codecs.Http.HttpVersion.Http11, HttpMethod.Post, uriFile.ToString()); // Use the PostBody encoder HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(factory, request, true); // true => 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 from previous request in formpost() bodyRequestEncoder.SetBodyHttpDatas(bodylist); // finalize request bodyRequestEncoder.FinalizeRequest(); var list = new List <object>(); // send request list.Add(request); // test if request was chunked and if so, finish the write if (bodyRequestEncoder.IsChunked) { list.Add(bodyRequestEncoder); } await channel.WriteAndFlushManyAsync(list); // Now no more use of file representation (and list of HttpData) bodyRequestEncoder.CleanFiles(); // Wait for the server to close the connection. await channel.CloseCompletion; }
public void HttpPostRequestEncoderSlicedBuffer() { var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost"); var encoder = new HttpPostRequestEncoder(request, true); // add Form attribute encoder.AddBodyAttribute("getform", "POST"); encoder.AddBodyAttribute("info", "first value"); encoder.AddBodyAttribute("secondinfo", "secondvalue a&"); encoder.AddBodyAttribute("thirdinfo", "short text"); const int Length = 100000; var array = new char[Length]; array.Fill('a'); string longText = new string(array); encoder.AddBodyAttribute("fourthinfo", longText.Substring(0, 7470)); FileStream fileStream1 = File.Open("./Multipart/file-01.txt", FileMode.Open, FileAccess.Read); this.files.Add(fileStream1); encoder.AddBodyFileUpload("myfile", fileStream1, "application/x-zip-compressed", false); encoder.FinalizeRequest(); while (!encoder.IsEndOfInput) { IHttpContent httpContent = encoder.ReadChunk(null); IByteBuffer content = httpContent.Content; int refCnt = content.ReferenceCount; Assert.True( (ReferenceEquals(content.Unwrap(), content) || content.Unwrap() == null) && refCnt == 1 || !ReferenceEquals(content.Unwrap(), content) && refCnt == 2, "content: " + content + " content.unwrap(): " + content.Unwrap() + " refCnt: " + refCnt); httpContent.Release(); } encoder.CleanFiles(); encoder.Close(); }
public void EncodeChunkedContent() { IHttpRequest req = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Post, "/"); HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(req, false); int length = 8077 + 8096; string longText = new string('a', length); encoder.AddBodyAttribute("data", longText); encoder.AddBodyAttribute("moreData", "abcd"); Assert.NotNull(encoder.FinalizeRequest()); while (!encoder.IsEndOfInput) { encoder.ReadChunk((IByteBufferAllocator)null).Release(); } Assert.True(encoder.IsEndOfInput); encoder.CleanFiles(); }