我正在尝试向.NET WebAPI发出相当简单的POST
请求。该请求是通过Azure函数发出的,以防万一。
代码如下:
public async Task SetBackupRecordAsync(BackupRecord backupRecord)
{
var content = new StringContent(JsonConvert.SerializeObject(backupRecord),
Encoding.UTF8,
"application/json");
var uri = new Uri(_httpClient.BaseAddress, "backup");
HttpResponseMessage result = null;
try
{
result = await _httpClient.PostAsync(uri, content).ConfigureAwait(false);
if (result.StatusCode == HttpStatusCode.Accepted)
{
return;
}
throw new Exception($"Unexpected response: The API responded with {(int) result.StatusCode} ({result.StatusCode}) when setting the backup record.");
}
catch (Exception exception)
{
// ...
}
}
这是包含的JSON内容:
{“ AddedById”:14000,“ AddedOnUtc”:“ 2019-06-17T09:43:25.9821306Z”,“ Backup”:false,“ DeletedById”:null,“ DeletedOnUtc”:null,“ Id”:“ 4c3ef086-3e2a-4964-bdc1-f5e72f525fbd“,” LastBackupUtc“:空,”名称“:”某物“}
我还尝试将每个属性设置为非null值,这没有区别。
与同一异常消息上的其他问题不同,此请求甚至都不会发送-称为_httpClient.PostAsync
时,会引发异常:
发送请求时出错。
内部异常提示
服务器犯了协议冲突。 Section = ResponseStatusLine
内部堆栈跟踪非常短:
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
一些其他要点:
GET
请求。那么这个请求怎么了?