0

I am trying to get message body from web service (located at http://www.ekkuli.net/conflict_call.php), which is returning 'Transfer-Encoding: chunked' tag and 409 Conflict response using the following code:

String webAddr = "http://www.ekkuli.net/conflict_call.php";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
httpWebRequest.ServicePoint.Expect100Continue = false;
httpWebRequest.Accept = "application/json";
httpWebRequest.KeepAlive = true;

try {
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
        String myJson = "{ \"method\" : \"just testing\" }";
        streamWriter.Write(myJson);
        streamWriter.Flush();
     }

     var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

     using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
         var responseText = streamReader.ReadToEnd(); 
      }
} catch (WebException ex) { // Catch 409 Conflict response
    try {
        // Try to get message body from 409 Response
        var s = ex.Response.GetResponseStream();
        var sr = new System.IO.StreamReader(s);
        var content = sr.ReadToEnd(); // THIS THROWS AN EXCEPTION
    } catch (Exception excep) {
        // Exception! Error getting response stream (ReadAsync): ReceiveFailure Value cannot be null. 
    }
}

Is there any way to get response body from 409 Conflict -response, when server is returning 'Transfer-encoding: Chuncked' -header? If this header is disabled and Content-length is returned instead, everything works fine, but in this case I cannot modify the server code.

Same problem occurs, when trying with RestSharp -library (it returns status code 0 and message body 0).

I am using Mono (for Mac).

jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • I'm getting `{"fault":{"message":"AlreadyExists: id = 219190"}}` from `var content = sr.ReadToEnd(); // THIS THROWS AN EXCEPTION`. Is this what you need? – jegtugado Sep 04 '18 at 07:59
  • Hi @john-ephraim-tugado, Yes, that's the correct response. For me the same line throws "Error getting response stream (ReadAsync): ReceiveFailure Value cannot be null." exception instead. Is it possible, that Mono vs. .NET makes the difference? – Jussi Gustafsson Sep 04 '18 at 08:22
  • What version of mono are you using? What .NET framework is it based on? – jegtugado Sep 04 '18 at 08:39
  • This is very interesting and I believe you should file an issue on Mono's [Github](https://github.com/mono/mono/issues) instead. – jegtugado Sep 04 '18 at 08:44
  • Thanks for the tip! I am using Mono 5.12.0.301 (64-bit). I think it is based on .NET Core runtime 2.1.2 and SDK version 2.1.302. – Jussi Gustafsson Sep 04 '18 at 08:45

0 Answers0