This paper addresses the distributed resource optimization issue in heterogeneous Web environments, where both resource nodes and service requests may be heterogeneous. Specifically, this paper presents an agent-based mechanism, where agents are employed to carry service requests. Agents are equipped with three behaviors, namely, least-loaded move, less-loaded move, and random move, to search for appropriate resource nodes. Every time, agents probalilistically choose a behavior to perform. As a whole, the multiagent system can accomplish the objective of load balancing and resource optimization. Through experiments on a computing platform, called SSADRO, we validate the effectiveness of the proposed mechanism. As compared to our previously proposed load balancing mechanism in [6], the one in this paper can address dynamic load balancing in heterogeneous environments.