HttpWebRequest was cancelled

asp.net, c#, xmlhttprequest

Solution

I have manage to overcome the error by implementing this:

        string requestData = (new StreamReader(Request.InputStream)).ReadToEnd();
        byte[] bytes = Request.ContentEncoding.GetBytes(requestData);

        Stream writer = webRequest.GetRequestStream();
        writer.Write(bytes, 0, bytes.Length);

I used Stream to write the `HttpWebRequest` instead of `StreamWriter` and read the `InputStream` in bytes using the `ContentEncoding`

Problem

I am using ASP.NET to read a Request.InputStream and send a HttpRequest to a page that was sent to me throw a QueryString parameter, I read correctly the Request.InputStream and I make the HttpRequest correctly, but when in my Request.InputStream I find special characters like this "Porsvägen" the I get this error: ystem.Net.WebException: The request was aborted: The request was canceled. ---> System.IO.IOException: Cannot close stream until all bytes are written. at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting) My code for getting the Request.InputStream is this: ``` string requestData = (new StreamReader(Request.InputStream)).ReadToEnd(); ``` My code for making a HttpRequest and sending it is this: ``` webRequest = (HttpWebRequest)WebRequest.Create(new Uri(urlDestination)); webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = requestData.Length; writer = new StreamWriter(webRequest.GetRequestStream()); writer.Write(requestData, 0, requestData.Length);//<-------Here I get the ERROR if (writer != null) { writer.Close(); } ``` Can you please tell me what is wrong with my code, because it works, but for special characters it crashes.

Original source