0

We are trying to write special char in post method but we are getting exception below

can not close the stream until all bytes are written

Below is the example code I am using and below are the special char which I am trying to write

internal static T InvokePostService<T>(string serviceUri, string _data)
{
    T _returnResponse = default(T);
    HttpWebRequest req = null;
    HttpWebResponse res = null;
    
    try
    {
        string url = serviceUri;
        req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "application/json; charset=utf-8";
        req.Timeout = 600000;
        req.Headers.Add("SOAPAction", url);
        req.ContentLength = _data.Length;
        var sw = new StreamWriter(req.GetRequestStream());
        sw.Write(_data);
        sw.Close();
        res = (HttpWebResponse)req.GetResponse();
        Stream responseStream = res.GetResponseStream();
        var streamReader = new StreamReader(responseStream);
        string responseString = streamReader.ReadToEnd();
        _returnResponse = JsonConvert.DeserializeObject<T>(responseString);
    }

Chars to write : •

gurkan
  • 884
  • 4
  • 16
  • 25
  • Did it help? https://stackoverflow.com/questions/19025834/cannot-close-stream-until-all-bytes-are-written-gooddata-api – Roman Ryzhiy Sep 04 '20 at 13:14
  • 1
    Use System.Net.WebUtility.HtmlEncode(string) and System.Net.WebUtility.HtmlDecode(string) – jdweng Sep 04 '20 at 13:15
  • Do as the error says, ignore @jdweng and don't close the stream until you've fired the request and read its response. Or, you know, use HttpClient and not the archaic HttpWebRequest. – CodeCaster Sep 04 '20 at 13:24

0 Answers0