问题
Using C# ASP.NET MVC, when calling a webservice on a Magento site:
Sometimes a Magento site will send wrong HTTP headers, specifically the body-size header saying that the body is bigger than it really is. For example it might say body-size=1000, but the body only consists of 999 bytes. Despite the header being incorrect, the body is correct so I would still like to be able to process it.
Now when I try to read that response in C#
var invoiceInfo = _magentoService.salesOrderInvoiceInfo(sessionId, invoiceId);
It throws an exception:
Connection closed by remote host
Which makes sense kindof, because its trying to read the 1000th byte but its not there. And to make it worse, the code that reads the body is buried deep within the .NET framework, so I cant change it:
[System.Web.Services.Protocols.SoapRpcMethodAttribute("urn:Mage_Api_Model_Server_V2_HandlerAction", RequestNamespace="urn:Magento", ResponseNamespace="urn:Magento")]
[return: System.Xml.Serialization.SoapElementAttribute("result")]
public salesOrderInvoiceEntity salesOrderInvoiceInfo(string sessionId, string invoiceIncrementId) {
object[] results = this.Invoke("salesOrderInvoiceInfo", new object[] {
sessionId,
invoiceIncrementId}); // exception thrown here
return ((salesOrderInvoiceEntity)(results[0]));
}
I can't change the Magento site, or fix whatever problem is causing this (its a 3rd parties webserver).
Is there anything I can do to change the behavior of my own C# code? I would like to be able to somehow force it to stop when it reaches the end of the body and ignore this exception if this case arises
回答1:
I don't know that this will work, but have you tried putting the code in a try / catch block and ignoring the exception? Does results == null?
Like this:
[System.Web.Services.Protocols.SoapRpcMethodAttribute("urn:Mage_Api_Model_Server_V2_HandlerAction", RequestNamespace="urn:Magento", ResponseNamespace="urn:Magento")]
[return: System.Xml.Serialization.SoapElementAttribute("result")]
public salesOrderInvoiceEntity salesOrderInvoiceInfo(string sessionId, string invoiceIncrementId) {
try
{
object[] results = this.Invoke("salesOrderInvoiceInfo", new object[] {
sessionId,
invoiceIncrementId}); // exception thrown here
}
catch(Exception) {}
if(results != null && results.Count() > 0) return ((salesOrderInvoiceEntity)(results[0]));
throw new Exception("results is null");
}
回答2:
Try solution from here (changing API endpoint to api.php): stackoverflow.com/a/36200828/1936722
Basically change the API url from:domain.com/index.php/api/v2_soap/index/wsdl/1
to:
domain.com/api/v2_soap/?wsdl=1
来源:https://stackoverflow.com/questions/13943733/dealing-with-incorrect-http-header-body-size-values