尝试将图片上传到eBay,以获取AddFixedPriceItem调用中的产品变体。
我已将代码复制到我的程序表单中:https://ebay.custhelp.com/app/answers/detail/a_id/1093/~/.net-sample-in-c%23-for-uploadsitehostedpictures
在调试过程中,我仔细检查了令牌,密钥和eBay(沙盒)URL是否全部用于沙箱并正确加载到变量中。我不确定我还需要做什么?
代码在遇到resp = (HttpWebResponse)req.GetResponse();
WebException:“远程服务器返回错误:(500)内部服务器错误”
这是我的代码(没有密钥):
静态字符串UploadSiteHostedPicture(string imageUrl) { string response = 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;
}
答案 0 :(得分:0)
我使用的是错误的沙盒网址。通常我使用SOAP,但我没有意识到它们对于XML调用是不同的。见这里:https://developer.ebay.com/DevZone/support/knowledgebase/
此外,上面的代码是错误的,因为它没有从XML响应中正确提取上传的图像URL(并且不返回它)。如果你想看到我的noob解决方案,那么这就是应该在try块中的代码(上图):
try
{
// get response and write to console
resp = (HttpWebResponse)req.GetResponse();
StreamReader responseReader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
string output = responseReader.ReadToEnd();
resp.Close();
//XmlDocument xmlResponse = new XmlDocument();
//xmlResponse.LoadXml(output);
string[] outputArray1 = Regex.Split(output, @"<FullURL>");
string urlNowAtFrontOfString = outputArray1[1];
string[] outputArray2 = Regex.Split(urlNowAtFrontOfString, @"</FullURL>");
response = outputArray2[0];
//process response
//show them how to get the full url and specify that in the AddItem request
}