0

我有以下 2 种方法,在一个类中,登录方法工作正常并检索和设置会话令牌,但为了让我使用 GetEvents(),我必须在 GetEvents() 的请求中发送 sessionToken。

但是在 getEvents() 的第 4 行代码(不包括注释和空格)上,我收到错误:对象引用未设置为对象的实例。

 The Entire Source can be downloaded here: (Copy and Paste into your browser)
 http://www.theebookzone.co.uk/betfairui.zip

任何想法我做错了什么?
任何帮助表示赞赏,即使它与此事没有直接关系。

public static string SessionToken = ""; // Set by Login();

static LoginResp Login()
    {
        // Make a new BFGS instance
        BFGlobal = new BFGlobalService.BFGlobalService();

        // Set up the request in [req]
        LoginReq req = new LoginReq();
        req.username = username;
        req.password = password;
        req.productId = productId;
        req.vendorSoftwareId = softwareId;

        // Set up the response in [resp]
        // Execute the call, and pass in the request
        LoginResp resp = BFGlobal.login(req);

        // Sets our public variable above to the recieved sessionToken
        SessionToken = resp.header.sessionToken;

        // return [resp] - which is the response from the call
        return resp;

    }

    public Array GetEvents()
    {
        // This will set the sessionToken declared at the top.
        LoginToBetfair();

        // Make a new instance of the web service
        BFGlobal = new BFGlobalService.BFGlobalService();

        // Load up the request
        GetEventsReq req = new GetEventsReq();

        // Error Line Below:
        req.header.sessionToken = SessionToken;  // <--- Here is where I get the error
        // Error Above Line: Object reference not set to an instance of an object.

        GetEventsResp resp = BFGlobal.getEvents(req);

        Array marketItems = resp.marketItems;

        return marketItems;

    }
4

2 回答 2

2

Web 服务通常是无状态的。为了存储会话,您应该使用 HTTPContext 缓存它。

于 2011-06-20T15:47:13.927 回答
1

我敢打赌 null 对象是headerof req.header。在该行放置一个断点,然后在变量调试器窗口中查看 req.header 的计算结果。如果它确实为空,那么您将不得不手动添加标题,例如

req.headers = new Headers();
于 2011-06-20T15:48:56.423 回答