سؤال

My web response content length always seem to be -1 after my web request. I'm sure you massage and signature are right. What am I doing wrong here?

            string msg = string.Format("{0}{1}{2}", nonce, clientId, apiKey);
            string signature = ByteArrayToString(SignHMACSHA256(apiSecret, StrinToByteArray(msg))).ToUpper();
            const string endpoint = "https://www.bitstamp.net/api/balance/";
            HttpWebRequest request = WebRequest.Create(endpoint) as HttpWebRequest;
            request.Proxy = null;
            request.Method = "POST";
            request.ContentType = "application/xml";
            request.Accept = "application/xml";
            request.Headers.Add("key", apiKey);
            request.Headers.Add("signature", signature);
            request.Headers.Add("nonce", nonce.ToString());
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
هل كانت مفيدة؟

المحلول 2

Got it working with webClient instead of the httpWebRequest. If someone can get it working with httpWebRequest, you wil get the answer.

            string msg = string.Format("{0}{1}{2}", nonce, clientId, apiKey);
            var signature = ByteArrayToString(SignHMACSHA256(apiSecret, StrinToByteArray(msg))).ToUpper();
            var path = "https://www.bitstamp.net/api/user_transactions/";

            using (WebClient client = new WebClient())
            {

                byte[] response = client.UploadValues(path, new NameValueCollection()
                {
                    { "key", apiKey },
                    { "signature", signature },
                    { "nonce", nonce.ToString()},

                });

                var str = System.Text.Encoding.Default.GetString(response);
            }

نصائح أخرى

From the documentation,

The ContentLength property contains the value of the Content-Length header returned with the response. If the Content-Length header is not set in the response, ContentLength is set to the value -1.

Because this worked with 'WebClient', there was nothing wrong with the request, which means almost definitely that the request was being sent back 'Chunked'. This is indicated by the header 'Transfer-Encoding'.

There are several reasons why the webserver might send back something chunked including the fact that the return is binary.

I came to this page because Fiddler was "interfering" with my request by turning a perfectly good response by the server and then returning it chunked to my client. That was because I had the 'Stream' button pushed or active. When it isn't, it sends the data back buffered which preserves the response from the server. That was a horrible thing to track down..

But the research did tell me about why the Content-Length header might be -1.

The solution? Either fix the way the server (or proxy in my case) is sending the response back, or just read the response stream to the end. The latter will return all the chunks to you connected and you can take a length of the bytes returned.

Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
String responseString = reader.ReadToEnd();
int responseLength = responseString.Length;

If you want bytes it is more involved -- not sure if there is a reader that allows you to read to the end -- the Binary reader requires a buffer up front.

An elegant way to consume (all bytes of a) BinaryReader?

Njoy.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top