HttpWebRequest 流中的错误超过了 Content-Length 字节大小
Error in HttpWebRequest stream exceed the Content-Length bytes size
这是将货物添加到 Orderhive API,身份验证是 AWS4 签名。
这是完整的错误:要写入流的字节数超过指定的 Content-Length 字节大小。
以下是导致错误的实现。
var headers = new Dictionary<string, string>
{
{AWS4SignerBase.X_Amz_Content_SHA256, contentHashString},
{"content-length", requestBody.Length.ToString()},
{"content-type", "application/json"},
{"id_token", _credentails.id_token},
{"X-Amz-Security-Token", _credentails.session_token}
};
public string InvokeHttpRequest(Uri endpointUri,
string httpMethod,
IDictionary<string, string> headers,
string requestBody)
{
string responseBody = "";
try
{
var request = ConstructWebRequest(endpointUri, httpMethod, headers);
if (!string.IsNullOrEmpty(requestBody))
{
var buffer = new byte[8192]; // arbitrary buffer size
var requestStream = request.GetRequestStream();
using (var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(requestBody)))
{
var bytesRead = 0;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
}
}
responseBody = CheckResponse(request);
}
catch (WebException ex)
{
using (var response = ex.Response as HttpWebResponse)
{
if (response != null)
{
var errorMsg = ReadResponseBody(response);
Console.WriteLine("\n-- HTTP call failed with exception '{0}', status code '{1}'", errorMsg, response.StatusCode);
}
}
}
return responseBody;
}
public HttpWebRequest ConstructWebRequest(Uri endpointUri,
string httpMethod,
IDictionary<string, string> headers)
{
var request = (HttpWebRequest)WebRequest.Create(endpointUri);
request.Method = httpMethod;
foreach (var header in headers.Keys)
{
// not all headers can be set via the dictionary
if (header.Equals("host", StringComparison.OrdinalIgnoreCase))
request.Host = headers[header];
else if (header.Equals("content-length", StringComparison.OrdinalIgnoreCase))
request.ContentLength = long.Parse(headers[header]);
else if (header.Equals("content-type", StringComparison.OrdinalIgnoreCase))
request.ContentType = headers[header];
else
request.Headers.Add(header, headers[header]);
}
return request;
}
我不知道如何更改代码来解决此问题。请帮忙。谢谢。
您指定的内容长度似乎与您写入上游的字节数不兼容。
当您设置内容长度时:
"content-length", requestBody.Length.ToString()
您发送的内容如下:
Encoding.UTF8.GetBytes(requestBody)
此处字符串的 Length
及其 UTF8 二进制表示中的字节数不同。
请尝试这样设置内容长度:
"content-length", Encoding.UTF8.GetByteCount(requestBody).ToString()
这是将货物添加到 Orderhive API,身份验证是 AWS4 签名。 这是完整的错误:要写入流的字节数超过指定的 Content-Length 字节大小。 以下是导致错误的实现。
var headers = new Dictionary<string, string>
{
{AWS4SignerBase.X_Amz_Content_SHA256, contentHashString},
{"content-length", requestBody.Length.ToString()},
{"content-type", "application/json"},
{"id_token", _credentails.id_token},
{"X-Amz-Security-Token", _credentails.session_token}
};
public string InvokeHttpRequest(Uri endpointUri,
string httpMethod,
IDictionary<string, string> headers,
string requestBody)
{
string responseBody = "";
try
{
var request = ConstructWebRequest(endpointUri, httpMethod, headers);
if (!string.IsNullOrEmpty(requestBody))
{
var buffer = new byte[8192]; // arbitrary buffer size
var requestStream = request.GetRequestStream();
using (var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(requestBody)))
{
var bytesRead = 0;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
}
}
responseBody = CheckResponse(request);
}
catch (WebException ex)
{
using (var response = ex.Response as HttpWebResponse)
{
if (response != null)
{
var errorMsg = ReadResponseBody(response);
Console.WriteLine("\n-- HTTP call failed with exception '{0}', status code '{1}'", errorMsg, response.StatusCode);
}
}
}
return responseBody;
}
public HttpWebRequest ConstructWebRequest(Uri endpointUri,
string httpMethod,
IDictionary<string, string> headers)
{
var request = (HttpWebRequest)WebRequest.Create(endpointUri);
request.Method = httpMethod;
foreach (var header in headers.Keys)
{
// not all headers can be set via the dictionary
if (header.Equals("host", StringComparison.OrdinalIgnoreCase))
request.Host = headers[header];
else if (header.Equals("content-length", StringComparison.OrdinalIgnoreCase))
request.ContentLength = long.Parse(headers[header]);
else if (header.Equals("content-type", StringComparison.OrdinalIgnoreCase))
request.ContentType = headers[header];
else
request.Headers.Add(header, headers[header]);
}
return request;
}
我不知道如何更改代码来解决此问题。请帮忙。谢谢。
您指定的内容长度似乎与您写入上游的字节数不兼容。
当您设置内容长度时:
"content-length", requestBody.Length.ToString()
您发送的内容如下:
Encoding.UTF8.GetBytes(requestBody)
此处字符串的 Length
及其 UTF8 二进制表示中的字节数不同。
请尝试这样设置内容长度:
"content-length", Encoding.UTF8.GetByteCount(requestBody).ToString()