HttpClient发送XML PostAsync格式错误的请求:过早结束文件?



我试图将xml发送到服务器,它总是抛出异常Malformed request: Premature end of file..。使用邮差它工作得很好,但使用HttpClient它没有,我认为问题是与headers服务器需要的是Content-Type", "application/xml,我不能做它的工作

我该如何修复它?

//HttpClient
HttpClient hClient = new HttpClient();            
hClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/xml");

//post            
var contentString = new StringContent(xml, Encoding.UTF8, "application/xml");
HttpResponseMessage response = hClient.PostAsync(URL_FINAL, contentString).GetAwaiter().GetResult();
var resultContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
xml>
<?xml version="1.0" encoding="utf-16"?>
<payment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<mode>default</mode>
<method>creditCard</method>
<sender>
<name>Meu Nome</name>
<email>email@gmail.com</email>
<phone>
<areaCode>17</areaCode>
<number>9999999999</number>
</phone>
<documents>
<document>
<type>CPF</type>
<value>9999999999</value>
</document>
</documents>
<hash>5e5240axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxea8a</hash>
</sender>
<currency>BRL</currency>
<notificationURL>https://dominio.com/StatusPagamentoTransaction</notificationURL>
<items>
<item>
<id>2</id>
<description>produto</description>
<quantity>2</quantity>
<amount>2.00</amount>
</item>
</items>
<extraAmount>0.00</extraAmount>
<reference>R748</reference>
<shipping>
<addressRequired>false</addressRequired>
</shipping>
<creditCard>
<token>91999999999999999999b0f</token>
<installment>
<quantity>1</quantity>
<value>2.00</value>
</installment>
<holder>
<name>nome proprietario cartao</name>
<documents>
<document>
<type>CPF</type>
<value>99999999999</value>
</document>
</documents>
<birthDate>18/12/1964</birthDate>
<phone>
<areaCode>17</areaCode>
<number>99999999</number>
</phone>
</holder>
<billingAddress>
<street>rua A</street>
<number>3333</number>
<district>bairro</district>
<city>cidade</city>
<state>SP</state>
<country>BRA</country>
<postalCode>05407002</postalCode>
</billingAddress>
</creditCard>
</payment>

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?><errors><error><code>11213</code><message>Malformed request: Premature end of file..</message></error></errors>

尝试设置Acceptheader

hClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

不是

hClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/xml");

最新更新