我正在尝试使用Walmart marketplace API上传项目(批量创建项目) 但总是得到响应500内部服务器错误。有任何想法吗? 其他Api功能还可以,但不是这个功能.. 实际上其他人使用的方法是“application / xml”,但这是使用“multipart / form-data;”
请注意,以下代码中的XML字符串/示例取自https://developer.walmartapis.com/#the-item-object
,然后我更改了ID属性。
public bool RequestUpdateItem()
{
string strBaseUrl = string.Format("https://marketplace.walmartapis.com/v2/feeds?feedType=item");
string strRequestMethod = "POST";
WalmartSignature Signature = new WalmartSignature(consumerId, privateEncodedStr, strBaseUrl, strRequestMethod);
string strSignature = string.Empty;
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
WalmartOrderModel orderModel = new WalmartOrderModel();
StringBuilder strBuilder = new StringBuilder();
bool bReturnValue = false;
try
{
strSignature = Signature.GetSignature(null); // null -> generate timestamp
httpWebRequest = (HttpWebRequest)WebRequest.Create(strBaseUrl);
httpWebRequest.Method = strRequestMethod;
httpWebRequest.Accept = "application/xml";
httpWebRequest.ContentType = "multipart/form-data;";
httpWebRequest.Headers.Add("WM_SVC.NAME", "Walmart Marketplace");
httpWebRequest.Headers.Add("WM_SEC.AUTH_SIGNATURE", strSignature);
httpWebRequest.Headers.Add("WM_CONSUMER.ID", Signature.ConsumerId);
httpWebRequest.Headers.Add("WM_SEC.TIMESTAMP", Signature.TimeStamp);
httpWebRequest.Headers.Add("WM_QOS.CORRELATION_ID", Guid.NewGuid().ToString());
string strRequestBody =
@"<?xml version=""1.0"" encoding=""UTF-8""?>
<MPItemFeed xmlns=""http://walmart.com/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://walmart.com/MPItem.xsd"">
<MPItemFeedHeader>
<version>2.1</version>
<requestId>333pecEl55nics</requestId>
<requestBatchId>333ecElect55cs38</requestBatchId>
</MPItemFeedHeader>
<MPItem>
<sku>BDK72wsd44f</sku>
<Product>
<productName>QVS</productName>
<longDescription>QVS</longDescription>
<shelfDescription>QVS</shelfDescription>
<shortDescription>QVS</shortDescription>
<mainImage>
<mainImageUrl>http://images.antonline.com/img-main/500/037229400328.jpg</mainImageUrl>
</mainImage>
<productIdentifiers>
<productIdentifier>
<productIdType>UPC</productIdType>
<productId>220740171234</productId>
</productIdentifier>
</productIdentifiers>
<productTaxCode>2038710</productTaxCode>
<additionalProductAttributes>
<additionalProductAttribute>
<productAttributeName>product_id_override</productAttributeName>
<productAttributeValue>true</productAttributeValue>
</additionalProductAttribute>
</additionalProductAttributes>
<Electronics>
<brand>QVS</brand>
<ElectronicsCables>
</ElectronicsCables>
</Electronics>
</Product>
<price>
<currency>USD</currency>
<amount>99.29</amount>
</price>
<shippingWeight>
<value>0.12</value>
<unit>LB</unit>
</shippingWeight>
</MPItem>
</MPItemFeed>";
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(strRequestBody);
writer.Flush();
stream.Position = 0;
httpWebRequest.ContentLength = stream.Length;
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
stream.CopyTo(requestStream);
//requestStream.Write(stream, 0, stream.Length);
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
Stream streamResponse = httpWebResponse.GetResponseStream();
var responseResult = XDocument.Load(streamResponse);
}
}
}
catch (Exception ex)
{
Console.WriteLine("RequestUpdateItem(): " + ex);
}
return bReturnValue;
}