Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I have a function like so:

private String SendRequest(String jsonRequest)
    {
        WebRequest webRequest = WebRequest.Create(_url);
        byte[] paramBytes = Encoding.UTF8.GetBytes(jsonRequest);
        byte[] responseBytes;

        webRequest.Method = "POST";
        webRequest.ContentType = "application/json";
        webRequest.ContentLength = paramBytes.Length;
        webRequest.Headers.Add("X-Transmission-Session-Id", _sessionId);


        using (Stream oStream = webRequest.GetRequestStream())
        {
            oStream.Write(paramBytes, 0, paramBytes.Length);
        }

        WebResponse webResponse = webRequest.GetResponse();

        using (Stream iStream = webResponse.GetResponseStream())
        {
            responseBytes = new byte[webResponse.ContentLength];
            iStream.Read(responseBytes, 0, (int) webResponse.ContentLength);
        }

        return Encoding.UTF8.GetString(responseBytes);
    }

The problem is, at the iStream.Read() stage, some of the bytes are lost. Using wireshark reveals all the bytes are sent to this machine, however .Net is loosing them somewhere along the way. In my current debugging session, for example, where webResponse.ContentLength = 4746 byte[3949] to byte[4745] are all 0's, but they should be populated. As a result, the UTF8 JSON string cuts off early and I can't deserialise my JSON.

I thought the code was pretty clear cut, I can't see where it's going wrong to loose those bytes.

Thanks for any help!

share|improve this question

2 Answers

up vote 0 down vote accepted

When reading from the stream you can get less bytes than requested.

http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

msdn example for WebResponse.GetResponseStream(): http://msdn.microsoft.com/en-us/library/system.net.webresponse.getresponsestream.aspx

share|improve this answer

I fixed it by using StreamReader instead :)

private String SendRequest(String jsonRequest)
    {
        WebRequest webRequest = WebRequest.Create(_url);
        byte[] paramBytes = Encoding.UTF8.GetBytes(jsonRequest);
        String jsonResponse;

        webRequest.Method = "POST";
        webRequest.ContentType = "application/json";
        webRequest.ContentLength = paramBytes.Length;
        webRequest.Headers.Add("X-Transmission-Session-Id", _sessionId);


        using (Stream oStream = webRequest.GetRequestStream())
        {
            oStream.Write(paramBytes, 0, paramBytes.Length);
            oStream.Close();
        }

        WebResponse webResponse = webRequest.GetResponse();

        using (Stream iStream = webResponse.GetResponseStream())
        {
            StreamReader reader = new StreamReader(iStream, Encoding.UTF8);
            jsonResponse = reader.ReadToEnd();
            reader.Close();
            iStream.Close();
        }

        return jsonResponse;
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.