0

尝试在 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;
}
4

1 回答 1

0

我使用了错误的沙盒 URL。通常我使用 SOAP,但我没有意识到它们对于 XML 调用是不同的。见这里:https ://developer.ebay.com/DevZone/support/knowledgebase/

此外,上面的代码是错误的,因为它没有正确地从 XML 响应中提取上传的图像 URL(并且没有返回它)。如果您想查看我的菜鸟解决方案,那么这是应该在 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
        }
于 2013-07-05T08:04:14.160 回答