尝试在 AddFixedPriceItem 调用中将图片上传到 eBay 以获取产品变体。
我已将代码复制到我的程序表单中:https ://ebay.custhelp.com/app/answers/detail/a_id/1093/~/.net-sample-in-c%23-for-uploadsitehostedpictures
在调试过程中,我仔细检查了令牌、密钥和 eBay(sandbox) URL 是否都用于沙箱并正确加载到变量中。我不确定我还需要做什么?
代码在命中时抛出异常resp = (HttpWebResponse)req.GetResponse();
WebException:“远程服务器返回错误:(500)内部服务器错误”
这是我的代码(没有密钥):
静态字符串 UploadSiteHostedPicture(string imageUrl) { 字符串响应 = string.Empty;
string token = ConfigurationManager.AppSettings["UserAccount.ApiToken"];
string SandboxOrProductionURL = ConfigurationManager.AppSettings["Environment.ApiServerUrl"];
string PictureURL = imageUrl;
string DevID = "Keys were removed before posting on stackoverflow";
string AppID = "Keys were removed before posting on stackoverflow";
string CertID = "Keys were removed before posting on stackoverflow";
string payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?> " +
"<UploadSiteHostedPicturesRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\">" +
"<ExternalPictureURL>" + PictureURL + "</ExternalPictureURL>" +
"<RequesterCredentials><eBayAuthToken>" + token + "</eBayAuthToken></RequesterCredentials>" +
"</UploadSiteHostedPicturesRequest>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(SandboxOrProductionURL);
HttpWebResponse resp = null;
//Add the request headers
req.Headers.Add("X-EBAY-API-COMPATIBILITY-LEVEL", "803");
req.Headers.Add("X-EBAY-API-SITEID", "0");
req.Headers.Add("X-EBAY-API-CALL-NAME", "UploadSiteHostedPictures");
req.Headers.Add("X-EBAY-API-DEV-NAME", DevID);
req.Headers.Add("X-EBAY-API-APP-NAME", AppID);
req.Headers.Add("X-EBAY-API-CERT-NAME", CertID);
//set the method to POST
req.Method = "POST";
//Convert the string to a byte array
byte[] postDataBytes = System.Text.Encoding.ASCII.GetBytes(payload);
int len = postDataBytes.Length;
req.ContentLength = len;
//Post the request to eBay
System.IO.Stream requestStream = req.GetRequestStream();
requestStream.Write(postDataBytes, 0, len);
requestStream.Close();
try
{
// get response and write to console
resp = (HttpWebResponse)req.GetResponse(); //THIS IS WHERE THE EXCEPTION OCCURS
StreamReader responseReader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
string output = responseReader.ReadToEnd();
resp.Close();
XmlDocument xmlResponse = new XmlDocument();
xmlResponse.LoadXml(output);
response = xmlResponse.ToString();
//process response
//show them how to get the full url and specify that in the AddItem request
}
catch (Exception ex)
{
MessageBox.Show("Exception caught from UploadSiteHostedPictures method:\r\n\r\n" + ex.Message);
}
return response;
}