I found my problem.
The order of my code caused the problem.
The solution is to call it in the following order:
- GetRequestStream (writing async to the stream) (the request is sent to the server after the first recording), and then:
- GetResponseAsync ()
- GetResponseStream ()
I understand that "GetResponseAsync" starts the client to send the request (currently only headers), but I found that it was an unreasonable step because the request was already sent after the first few bits were written to the stream.
The second reason for my problems is Fiddler, but Fiddler only supports response streams, not requests.
The code obtained by reference is the HttpWebRequest class:
HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://xxx") as HttpWebRequest; httpWebRequest.Method = "POST"; httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("user:pw")); httpWebRequest.PreAuthenticate = true; httpWebRequest.SendChunked = true; httpWebRequest.AllowWriteStreamBuffering = false; httpWebRequest.AllowReadStreamBuffering = false; httpWebRequest.ContentType = "application/octet-stream"; Stream st = httpWebRequest.GetRequestStream(); Console.WriteLine("Go"); try { st.Write(buffer, 0, buffer.Length); //with the first write, the request will be send. st.Write(buffer, 0, buffer.Length); st.Write(buffer, 0, buffer.Length); for (int i = 1; i <= 10; i++) { st.Write(buffer, 0, buffer.Length); //still writing while I can read on the stream at my ASP.NET web api } } catch (WebException ex) { var y = ex.Response; } finally { st.Close(); } // Now we can read the response from the server in chunks Task<WebResponse> response = httpWebRequest.GetResponseAsync(); Stream resultStream = response.Result.GetResponseStream(); byte[] data = new byte[1028]; int bytesRead; while ((bytesRead = resultStream.Read(data, 0, data.Length)) > 0) { string output = System.Text.Encoding.UTF8.GetString(data, 0, bytesRead); Console.WriteLine(output); }
Code obtained by reference, class "HttpClient":
HttpClientHandler ch = new HttpClientHandler(); HttpClient c = new HttpClient(ch); c.DefaultRequestHeaders.TransferEncodingChunked = true; c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes("user:pw"))); Stream stream = new MemoryStream(); AsyncStream asyncStream = new AsyncStream(); // Custom implementation of the PushStreamContent with the method, "WriteToStream()". PushStreamContent streamContent = new PushStreamContent(asyncStream.WriteToStream); HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod("POST"), "http://XXX") { Content = streamContent }; requestMessage.Headers.TransferEncodingChunked = true; HttpResponseMessage response = await c.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead); // The request has been sent, since the first write in the "WriteToStream()" method. response.EnsureSuccessStatusCode(); Task<Stream> result = response.Content.ReadAsStreamAsync(); byte[] data = new byte[1028]; int bytesRead; while ((bytesRead = await result.Result.ReadAsync(data, 0, data.Length)) > 0) { string output = System.Text.Encoding.UTF8.GetString(data, 0, bytesRead); Console.WriteLine(output); } Console.ReadKey();
user437899
source share