我正在尝试使用HttpClient调用一个端点。请求数据格式为XML。这是代码
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
string fullURL = $"{_baseAPISettings.Value.BaseURL}{_blackListAPISettings.Value.APIEndpoint}";
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, fullURL);
_client.DefaultRequestHeaders.Add(AppConstants.SOAPHeader, AppConstants.SOAPHEaderValue);
string authorizationInfo = Convert.ToBase64String(Encoding.Default.GetBytes($"{_baseAPISettings.Value.User} : {_baseAPISettings.Value.Password}"));
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", authorizationInfo);
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(AppConstants.TextXML));
string requestXMLDocument = _httpRequestXMLCreator.CreateBlackListXMLRequestObject(playerCPR);
requestMessage.Content = new StringContent(requestXMLDocument, Encoding.UTF8, AppConstants.TextXML);
httpResponseMessage = await _client.PostAsXmlAsync(fullURL, requestMessage);
这段代码产生了上面的错误,到目前为止我还不能理解它只在JSON中工作。。。有什么帮助吗?
System.Runtime.Serialization.InvalidDataContractException:无法序列化类型System.Net.Http.StringContent。请考虑使用DataContractAttribute属性标记它,并使用DataMemberAttribute属性标记要序列化的所有成员。或者,您可以确保该类型是公共的,并且具有无参数构造函数——然后将序列化该类型的所有公共成员,并且不需要任何属性