2
|a b |
a := ZnClient new.
a get: 'http://cloud-storage.com/login'.
a
formAt: 'username' put: 'jom';
formAt: 'password' put: 'mypass';
post;
get: 'http://cloud-storage.com/my-file'.
"Here I want to refresh the session for every 60sec and"
"to checking for newer data"
b := a maxNumberOfRedirects:60
Transcript show: b; cr.

我想实现一种方法,可以每 60 秒刷新一次 ZnClient 会话,以检查我登录的服务器上的更新数据。我尝试了pharo的重定向方法,但它似乎不起作用。或者说它没有显示任何东西。任何想法?

4

1 回答 1

2
| session data |

session := ZnClient new url: 'http://cloud-storage.com'.

"Login"
session path: '/login';
    formAt: 'email' put: 'jom';
    formAt: 'password' put: 'mypass';
    post.

"Get data"
data := session path: '/my-file'; get; contents.

"Check for new data every 60 secs for maximum 100 tries"
[
    100 timesRepeat: [
        | newData |
        (Delay forSeconds: 60) wait.
        newData := session path: '/my-file'; get; contents.
        (data ~= newData) ifTrue: [Transcript show: newData; cr]
    ]
] fork.

注意。尽管有上述示例代码,您可能需要考虑If-Modified-Since在 ZnClient 中尝试方法。

于 2017-11-25T13:54:53.753 回答