简体   繁体   English

HttpWebResponse contentLength总是-1

[英]HttpWebResponse contentLength always -1

My web response content length always seem to be -1 after my web request. 在我的网络请求之后,我的网络响应内容长度似乎总是-1。 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();

From the documentation , 文档中

The ContentLength property contains the value of the Content-Length header returned with the response. ContentLength属性包含随响应返回的Content-Length标头的值。 If the Content-Length header is not set in the response, ContentLength is set to the value -1. 如果未在响应中设置Content-Length标头,则ContentLength将设置为值-1。

Got it working with webClient instead of the httpWebRequest. 使用webClient而不是httpWebRequest。 If someone can get it working with httpWebRequest, you wil get the answer. 如果有人可以使用httpWebRequest,你会得到答案。

            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);
            }

Because this worked with 'WebClient', there was nothing wrong with the request, which means almost definitely that the request was being sent back 'Chunked'. 因为这与'WebClient'一起工作,所以请求没有任何问题,这几乎肯定意味着请求被发回'Chunked'。 This is indicated by the header 'Transfer-Encoding'. 这由标题“Transfer-Encoding”表示。

There are several reasons why the webserver might send back something chunked including the fact that the return is binary. 有几个原因可以解释为什么Web服务器可能会发回一些块,包括返回是二进制的。

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. 我来到这个页面是因为Fiddler通过服务器转动一个非常好的响应然后将它返回到我的客户端来“干扰”我的请求。 That was because I had the 'Stream' button pushed or active. 那是因为我按下了“Stream”按钮或激活了它。 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. 但研究确实告诉我为什么Content-Length标头可能为-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? 一种优雅的消费方式(a的所有字节)BinaryReader?

Njoy. 恩乔伊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM