English 中文(简体)
使用HTTPClient的InvalidDataContract异常
原标题:InvalidDataContract exception using HTTPClient

我正在尝试使用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属性标记要序列化的所有成员。或者,您可以确保该类型是公共的,并且具有无参数构造函数——然后将序列化该类型的所有公共成员,并且不需要任何属性

问题回答

更改以下代码

httpResponseMessage = await _client.PostAsXmlAsync(fullURL, requestMessage);

httpResponseMessage = await _client.PostAsXmlWithSerializerAsync(fullURL, requestMessage);

We need 到 create a cus到m extension method which explicitly specifies 到 use XmlSerializer.

public static class HttpExtensions
{
    public static Task<HttpResponseMessage> PostAsXmlWithSerializerAsync<T>(this HttpClient client, Uri requestUri, T value, CancellationToken cancellationToken)
    {
        return client.PostAsync(requestUri, value,
                      new XmlMediaTypeFormatter { UseXmlSerializer = true },
                      cancellationToken);
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签