HttpClient 发送 XML PostAsync 格式错误的请求:文件过早结束?
HttpClient send XML PostAsync Malformed request: Premature end of file?
我正在尝试将 xml 发送到服务器,但它总是抛出异常 Malformed request: Premature end of file..
。使用 Postman 效果很好,但使用 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>
尝试设置Accept
header
hClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
而不是
hClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/xml");
我正在尝试将 xml 发送到服务器,但它总是抛出异常 Malformed request: Premature end of file..
。使用 Postman 效果很好,但使用 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>
尝试设置Accept
header
hClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
而不是
hClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/xml");