c#eBay Shopping API

时间:2015-09-23 16:02:55

标签: c# ebay shopping

从使用下面的代码我得到对象引用错误。我的控制台输出语句有问题吗?这只是完整代码的简要摘录。

     CustomShopping service2 = new CustomShopping();           
                    service2.Url = "http://open.api.ebay.com/shopping";
            GetSingleItemResponseType response3 = service2.GetSingleItem(request2);
            request2.IncludeSelector = "Details";
            request2.ItemID = "181878344284";
            Console.WriteLine("Quantity: " + response3.Item.Quantity);


Custom Class file:


using System;
using System.Collections.Generic;
using System.Text;
using shoppingtesterase.com.ebay.developer;
using System.Net;

namespace shoppingtesterase
{
    class CustomShopping : Shopping
    {
        protected override System.Net.WebRequest GetWebRequest(Uri uri)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);
                request.Headers.Add("X-EBAY-API-APP-ID", "???????????");
                request.Headers.Add("X-EBAY-API-CALL-NAME", "GetSingleItem");
                request.Headers.Add("X-EBAY-API-REQUEST-ENCODING", "SOAP");
                request.Headers.Add("X-EBAY-API-VERSION", "897");
                request.Headers.Add("X-EBAY-API-SITE-ID", "0");
                request.Headers.Add("X-EBAY-API-RESPONSE-ENCODING", "SOAP");
                return request;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

对象未设置/ null引用很难处理。

在对象图中,response3.Item.Quantity,某些东西为null。如果"项目"为null然后您不能引用数量。 C#不会做:response3。" null" .Quantity

错误检查会发生什么:

if (response3 == null || response3.Item == null || response3.Item.Quantity == null)
{
    // log error
    // maybe throw exception?
    throw new Exception("Service Response contains null values.");
}