1
我尝试使用WebClient将数据发送到webapi。运行我的代码会在标题中显示异常。 有人可以帮我吗?'Content-Length'标题必须使用适当的属性或方法进行修改。参数名称:名称
我用头定义
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", "[email protected]"));
wc.Credentials = wrCache;
byte[] htmlResult = wc.UploadData(URI, "POST", data);