计算内容长度
本文关键字:计算 | 更新日期: 2023-09-27 18:33:31
我在制作 http POST 时遇到问题。我正在调用的 API 正在请求content-length
。但我不确定该怎么做。
这是我的代码:
public static string publishClip(string instance_url, string sessionId, string clipId)
{
int trev = System.Text.ASCIIEncoding.Unicode.GetByteCount(instance_url + "/services/apexrest/DesktopClient/PublishClip/" + clipId);
WebRequest wrGETURL;
wrGETURL = WebRequest
.Create(instance_url
+ "/services/apexrest/DesktopClient/PublishClip/"
+ clipId);
wrGETURL.Method = "POST";
wrGETURL.ContentType = "application/json";
wrGETURL.ContentLength = trev;
wrGETURL.Headers.Add("Authorization", "Bearer " + sessionId);
Stream objStreamclipId = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStreamclipId);
return "trev";
}
谁能帮我?
这是我得到的错误:
{"如果设置 ContentLength>0 或 SendChunked==true,则必须提供请求正文。 通过在 [Begin]GetResponse 之前调用 [Begin]GetRequestStream 来执行此操作。
var postData = ?;
byte[] bytes = encoding.GetBytes(postData);
wrGETURL.ContentLength = bytes.Length;
你的postData
是特雷夫吗?
您需要从 StreamReader objReader 获取字节数。为此,请参阅此处: 如何在 C# 中将流转换为 byte[]?并数一数。