我正在尝试使用沃尔玛市场 API(批量创建商品)上传商品,但总是收到 500 内部服务器错误的响应。有任何想法吗?其他 Api 函数还可以,但不是这个函数。实际上其他人正在使用方法是“application/xml”,但这是使用“multipart/form-data;”
请注意,下面代码中的 XML 字符串/示例取自https://developer.walmartapis.com/#the-item-object
,然后我更改了 IDs 属性。
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;
}