gpt4 book ai didi

webclient - 必须使用适当的属性或方法修改 'Content-Length' header 。参数名称 : name

转载 作者:行者123 更新时间:2023-12-04 13:04:15 26 4
gpt4 key购买 nike

我尝试使用 WebClient 将数据发送到 Web api。
运行我的代码会出现标题中显示的异常。
有人能帮我吗?
这是我的代码的一部分,带有 Header Definition

using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
wc.Headers[HttpRequestHeader.Authorization] = headerString;

data = Encoding.UTF8.GetBytes(jsonData);
string contentLength = data.Length.ToString();

wc.Headers[HttpRequestHeader.ContentLength] = contentLength;
wc.Headers[HttpRequestHeader.Accept] = "application/json";

wrCache = new CredentialCache();
wrCache.Add(new Uri(URI), "Basic", new NetworkCredential("user1", "f_k@1F7"));

wc.Credentials = wrCache;


byte[] htmlResult = wc.UploadData(URI, "POST", data);

最佳答案

您可以使用 HttpWebRequest .例如:

private static HttpStatusCode UploadSharepointFile(string fileName)
{
// Read file from path
byte[] buffer = File.ReadAllBytes(fileName);

HttpWebRequest http =
WebRequest.CreateHttp($"https://google.com");

http.Method = "POST";

http.Credentials = (ICredentials) Program.cred;

http.Headers.Add("Accept", "application/json");
http.ContentLength = buffer.Length;
http.GetRequestStream().Write(buffer, 0, buffer.Length);

HttpWebResponse response = (HttpWebResponse) http.GetResponse();
return response.StatusCode;
}

关于webclient - 必须使用适当的属性或方法修改 'Content-Length' header 。参数名称 : name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42508911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com